Android的UI - 动态添加按钮的GridView [英] Android UI - add buttons dynamically to Gridview

查看:255
本文介绍了Android的UI - 动态添加按钮的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我现在卡住了。我停留在动态地添加按钮的GridView。结果
- 我的gridview的恒星一键结果。
- 当用户点击该按钮上下文菜单,弹出,要求用户输入的信息,一旦做到这一点。在网格视图块与该信息创建结果
- 这显示在画面

我已经粘贴了code。我没有对如何做到这一点清晰的思路。我假设它会被夸大一个新的视图并将其添加到父(布局或网格?)视图。我没有对如何code它更主意。我从谷歌尝试了许多东西。我以为我可以从简单的图像gridview的开始,它修改为我的需要,但没有奏效。

请提供一些方向。

create_team_new.xml(布局在那里的GridView所在)

 <?XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的
    机器人:ID =@ + ID / parentcreateteam
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
     机器人:背景=@绘制/背景><的TextView
    机器人:ID =@ + ID / citytextview
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=@字符串/城
    机器人:layout_alignTop =@ + ID / TeamCity的
    机器人:layout_alignParentLeft =真/>< AutoCompleteTextView
    机器人:ID =@ + ID / teamname
    机器人:layout_width =244dp
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignLeft =@ + ID / TeamCity的
    机器人:layout_alignRight =@ + ID / gridviewplayer
    机器人:文字=@字符串/ teamname
    机器人:TEXTSIZE =18sp>
< requestFocus的/>
< / AutoCompleteTextView>
<按钮
    机器人:ID =@ + ID / createteam
    机器人:layout_width =140dp
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=创建组
    机器人:layout_alignParentBottom =真
    机器人:layout_centerHorizo​​ntal =真
     机器人:背景=@绘制/ red_button
          风格=@ +风格/ BUTTON_TEXT/><的TextView
    机器人:ID =@ + ID / teamnametextview
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =22dp
    机器人:layout_alignBaseline =@ + ID / teamname
    机器人:layout_alignBottom =@ + ID / teamname
    机器人:layout_alignParentLeft =真
    机器人:文字=@字符串/ teamname/>< AutoCompleteTextView
    机器人:ID =@ + ID / TeamCity的
    机器人:layout_width =244dp
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignLeft =@ + ID /蟋蟀
    机器人:layout_alignRight =@ + ID / gridviewplayer
    机器人:layout_below =@ + ID / teamname
    机器人:layout_marginTop =22dp
    机器人:EMS =10
    机器人:文字=@字符串/城
    机器人:TEXTSIZE =18sp>
< / AutoCompleteTextView><的TextView
    机器人:ID =@ + ID / radiotextview
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentLeft =真
    机器人:layout_below =@ + ID / TeamCity的
    机器人:layout_marginTop =15dp
    机器人:文字=选择一个运动/>
< GridView控件
    机器人:ID =@ + ID / gridviewplayer
    机器人:layout_width =match_parent
    机器人:layout_height =170dp
    机器人:layout_above =@ + ID / createteam
    机器人:layout_below =@ + ID / pick_sport
    机器人:layout_centerHorizo​​ntal =真
    机器人:背景=@绘制/ gv_bkg
 机器人:填充=5DP
    机器人:为numColumns =4>< / GridView的>< RadioGroup中
    机器人:ID =@ + ID / pick_sport
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT    机器人:layout_alignRight =@ + ID / createteam
    机器人:layout_below =@ + ID / radiotextview
    机器人:方向=横向>    <单选
        机器人:ID =@ + ID /蟋蟀
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=促织/>    <单选
        机器人:ID =@ + ID /足球
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=足球/>
< / RadioGroup中>< / RelativeLayout的>

gv_createplayer.xml(查看里面的GridView充气)

 <?XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    ><按钮机器人:ID =@ + ID / btn_player
    机器人:layout_width =match_parent    机器人:layout_height =WRAP_CONTENT    机器人:文字=+    机器人:背景=@绘制/ red_button>< /按钮>
< / LinearLayout中>

PlayerAdapter.java(gridview的适配器)

 进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.Button;
进口android.widget.GridView;
进口android.widget.ImageView;公共类PlayerAdapter延伸BaseAdapter {    私人语境mContext;
    LayoutInflater吹气;    //请阵列中的所有图片
    公共整数[] mThumbIds = {};
        / * R.drawable.gridview_createteam,R.drawable.pic2,
            R.drawable.pic3,R.drawable.pic4,
            R.drawable.pic5,R.drawable.pic6,
            R.drawable.pic7,R.drawable.pic8,
            R.drawable.pic9,R.drawable.pic10,
            R.drawable.pic11,R.drawable.pic12,
            R.drawable.pic13,R.drawable.pic14,
            R.drawable.pic15
    };
* /
    //构造
    公共PlayerAdapter(上下文C){
        mContext = C;
    }    @覆盖
    公众诠释的getCount(){
        返回mThumbIds.length;
    }    @覆盖
    公共对象的getItem(INT位置){
        返回mThumbIds [位置]
    }    @覆盖
    众长getItemId(INT位置){
        返回0;
    }    @燮pressWarnings(静态访问)
    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        查看查看=新景(mContext);
        view.inflate(mContext,R.layout.gv_createplayer,NULL);        返回视图。
    }
}


解决方案

  

1)如何创​​建一个列表视图主视图内,只使用屏幕的一半。


这是由重量属性来完成。为您的草图粗略的例子:前包装(edittexts和按钮)的。45的权重,在ListView有。45的重量和页脚有.1重量。您可以指定在周围的视图重总和;但是默认的应该是1。


  

2)如何填充网格的方式列表视图(两列一行会重演)


好吧,与的GridView (适配器API是相同)(

This is where I am stuck now. I am stuck at dynamically adding buttons to gridview.
- My gridview stars with a one button.
- When user clicks that button a context menu , pops up, asking user to enter info, once that is done. A block in the grid view is created with that info
- This is shown in the picture

I have pasted the code. I do not have clear idea on how to do it. I am assuming it will be inflating a new view and adding it to parent(layout or grid ?) view. I do not have much idea on how to code it. I have tried many things from google. I thought I could start from simple image gridview and modify it to my needs but it did not work.

Please provide some directions.

create_team_new.xml(layout where gridview resides)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/parentcreateteam"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
     android:background="@drawable/background">

<TextView
    android:id="@+id/citytextview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/city"
    android:layout_alignTop="@+id/teamcity"
    android:layout_alignParentLeft="true" />

<AutoCompleteTextView
    android:id="@+id/teamname"
    android:layout_width="244dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/teamcity"
    android:layout_alignRight="@+id/gridviewplayer"
    android:text="@string/teamname"
    android:textSize="18sp" >
<requestFocus />
</AutoCompleteTextView>
<Button
    android:id="@+id/createteam"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:text="Create Team"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
     android:background="@drawable/red_button"
          style="@+style/button_text" />

<TextView
    android:id="@+id/teamnametextview"
    android:layout_width="wrap_content"
    android:layout_height="22dp"
    android:layout_alignBaseline="@+id/teamname"
    android:layout_alignBottom="@+id/teamname"
    android:layout_alignParentLeft="true"
    android:text="@string/teamname" />

<AutoCompleteTextView
    android:id="@+id/teamcity"
    android:layout_width="244dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/cricket"
    android:layout_alignRight="@+id/gridviewplayer"
    android:layout_below="@+id/teamname"
    android:layout_marginTop="22dp"
    android:ems="10"
    android:text="@string/city"
    android:textSize="18sp" >


</AutoCompleteTextView>

<TextView
    android:id="@+id/radiotextview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/teamcity"
    android:layout_marginTop="15dp"
    android:text="Pick a Sport" />


<GridView
    android:id="@+id/gridviewplayer"
    android:layout_width="match_parent"
    android:layout_height="170dp"
    android:layout_above="@+id/createteam"
    android:layout_below="@+id/pick_sport"
    android:layout_centerHorizontal="true"
    android:background="@drawable/gv_bkg"
 android:padding="5dp"
    android:numColumns="4" >

</GridView>



<RadioGroup
    android:id="@+id/pick_sport"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_alignRight="@+id/createteam"
    android:layout_below="@+id/radiotextview"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/cricket"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cricket" />

    <RadioButton
        android:id="@+id/soccer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Soccer" />
</RadioGroup>

</RelativeLayout>

gv_createplayer.xml(view to be inflated inside gridview)

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<Button  android:id="@+id/btn_player"
    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:text= "+"

    android:background="@drawable/red_button">

</Button>
</LinearLayout>

PlayerAdapter.java(gridview Adapter)

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;

public class PlayerAdapter extends BaseAdapter {

    private Context mContext;
    LayoutInflater inflater;

    // Keep all Images in array
    public Integer[] mThumbIds = {};
        /*  R.drawable.gridview_createteam, R.drawable.pic2,
            R.drawable.pic3, R.drawable.pic4,
            R.drawable.pic5, R.drawable.pic6,
            R.drawable.pic7, R.drawable.pic8,
            R.drawable.pic9, R.drawable.pic10,
            R.drawable.pic11, R.drawable.pic12,
            R.drawable.pic13, R.drawable.pic14,
            R.drawable.pic15
    };
*/
    // Constructor
    public PlayerAdapter(Context c){
        mContext = c;
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressWarnings("static-access")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = new View(mContext);
        view.inflate(mContext, R.layout.gv_createplayer,null);

        return view;


    }


}

解决方案

1) How to create a list view inside a main view, only using a half of the screen.

This is done by the weight attribute. A rough example for your sketch: The top wrapper (edittexts and buttons) has a weight of .45, the listview has a weight of .45 and the footer has a weight of .1. You can specify the weight-sum in the surrounding view; but default should be 1.

2) How to populate list view in grid manner (a row of two columns will repeat itself)

Well, with the GridView (the adapter API is the same) (:

这篇关于Android的UI - 动态添加按钮的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆