GridView跟ListView都是比较常用的多控件布局,可以进行动态添加排版,可以根据数据来动态的添加行或者列
GridView常用的XML属性:
属性名称 | 描述 |
android:columnWidth | 设置列的宽度。 |
android:gravity | 设置此组件中的内容在组件中的位置。可选的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical可以多选,用“|”分开。 |
android:horizontalSpacing | 两列之间的间距。 |
android:numColumns | 设置列数。 |
android:stretchMode | 缩放模式。 |
android:verticalSpacing | 两行之间的间距。 |
下面来实现为底部动态添加列按钮,为已到场添加行
layout:conference_management(主页面),grid_button_item(设置底部按钮的样式),grid_present_item(设置已到场每行的样式)
activity:ConferenceManagementActivity
adapter:GridBottomAdapter,GridPresentAdapter(这两个的区别是添加一个数据和两个数据)
data:GridInfo(通过get,set获取数据)
conference_management:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4.2"
android:orientation="horizontal"
android:background="#2346df">
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:text="会场管理"
android:textSize="18dip"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4.2"
android:orientation="horizontal"
android:background="#2346df"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/presentButton"
android:layout_weight="1"
android:text="已到场"
android:textSize="18sp"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/notPresentButton"
android:layout_weight="1"
android:text="未到场"
android:textSize="18sp"
/>
</LinearLayout>
<!-- <GridView
android:id="@+id/gridTop"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:gravity="center"
android:cacheColorHint="#00000000"
android:listSelector="#00000000"
>
</GridView>-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.3">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/presentLayout"
android:visibility="visible"
>
<GridView
android:id="@+id/presentGrid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:gravity="center"
android:cacheColorHint="#00000000"
android:listSelector="#00000000"
>
</GridView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/notPresentLayout"
android:visibility="gone"
>
<GridView
android:id="@+id/notPresentGrid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:gravity="center"
android:cacheColorHint="#00000000"
android:listSelector="#00000000"
>
</GridView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4"
>
<GridView
android:id="@+id/gridBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="0dip"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:gravity="center"
android:cacheColorHint="#00000000"
android:listSelector="#00000000"
android:background="#a678ff"
>
</GridView>
</LinearLayout>
</LinearLayout>
grid_button_item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutButton"
android:layout_width="fill_parent"
android:layout_height="45dip"
android:layout_gravity="center">
<Button
android:id="@+id/menuImage"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:textColor="#ffffff"
android:layout_centerVertical="true"
android:text="button"
>
</Button>
</RelativeLayout>
present_item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/present_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_weight="1.5"
/>
<TextView
android:id="@+id/present_phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="18sp"
/>
<Button
android:id="@+id/present_remove"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="移出"
android:textSize="18sp"
/>
<Button
android:id="@+id/present_speak"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="发言"
android:textSize="18sp"
/>
</LinearLayout>
GridInfo:
public class GridInfo {
private String name;
private String present_name,present_phone;
private String notPresent_name,notPresent_phone,notPresent_state;
public GridInfo() {
}
public GridInfo(String name) {
this.name = name;
}
public GridInfo(String present_name,String present_phone) {
this.present_name=present_name;
this.present_phone=present_phone;
}
public GridInfo(String notPresent_name,String notPresent_phone,String notPresent_state) {
this.notPresent_name=notPresent_name;
this.notPresent_phone=notPresent_phone;
this.notPresent_state=notPresent_state;
}
//Button
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//已到场
public String getPresentName() {
return present_name;
}
public void setPresentName(String present_name) {
this.present_name = present_name;
}
public String getPresentPhone() {
return present_phone;
}
public void setPresentPhone(String present_phone) {
this.present_phone = present_phone;
}
//未到场
public String getNotPresentName() {
return notPresent_name;
}
public void setNotPresentName(String notPresent_name) {
this.notPresent_name = notPresent_name;
}
public String getNotPresentPhone() {
return notPresent_phone;
}
public void setNotPresentPhone(String notPresent_phone) {
this.notPresent_phone = notPresent_phone;
}
public String getNotPresentState() {
return notPresent_state;
}
public void setNotPresentState(String notPresent_state) {
this.notPresent_state = notPresent_state;
}
}
GridBottomAdapter:
public class GridBottomAdapter extends BaseAdapter {
private class GridHolder {
Button button;
}
private Context context;
private List<GridInfo> list;
private LayoutInflater mInflater;
public GridBottomAdapter(Context c) {
super();
this.context = c;
}
public void setList(List<GridInfo> list) {
this.list = list;
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int index) {
return list.get(index);
}
@Override
public long getItemId(int index) {
return index;
}
@Override
public View getView(int index, View convertView, ViewGroup parent) {
GridHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grid_bottom_item, null);
holder = new GridHolder();
holder.button = (Button)convertView.findViewById(R.id.menuImage);
holder.button.setClickable(false);
convertView.setTag(holder);
}else{
holder = (GridHolder) convertView.getTag();
}
GridInfo info = list.get(index);
if (info != null) {
holder.button.setText(info.getName());
}
return convertView;
}
}
GridPresentAdapter
public class GridPresentAdapter extends BaseAdapter {
private class GridHolder {
TextView present_name;
TextView present_phone;
}
private Context context;
private List<GridInfo> list;
private LayoutInflater mInflater;
public GridPresentAdapter(Context c) {
super();
this.context = c;
}
public void setList(List<GridInfo> list) {
this.list = list;
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int index) {
return list.get(index);
}
@Override
public long getItemId(int index) {
return index;
}
@Override
public View getView(int index, View convertView, ViewGroup parent) {
GridHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.present_item, null);
holder = new GridHolder();
holder.present_name = (TextView)convertView.findViewById(R.id.present_name);
holder.present_phone = (TextView)convertView.findViewById(R.id.present_phone);
convertView.setTag(holder);
}else{
holder = (GridHolder) convertView.getTag();
}
GridInfo info = list.get(index);
if (info != null) {
holder.present_name.setText(info.getPresentName().toString());
holder.present_phone.setText(info.getPresentPhone().toString());
}
return convertView;
}
}
ConferenceManagementActivity:主activity
public class ConferenceManagementActivity extends Activity{
private GridView presentGrid,notPresentGrid,gridBottom;
private LinearLayout presentLayout,notPresentLayout;
private ArrayList<GridInfo> list;
private GridBottomAdapter bottomAdapter;
private GridPresentAdapter presentAdapter;
private GridNotPresentAdapter notPresentAdapter;
private Button presentButton,notPresentButton;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.conference_management);
presentButton=(Button) findViewById(R.id.presentButton);
notPresentButton=(Button) findViewById(R.id.notPresentButton);
presentGrid=(GridView) findViewById(R.id.presentGrid);
notPresentGrid=(GridView) findViewById(R.id.notPresentGrid);
gridBottom=(GridView) findViewById(R.id.gridBottom);
presentLayout=(LinearLayout) findViewById(R.id.presentLayout);
notPresentLayout=(LinearLayout) findViewById(R.id.notPresentLayout);
addBottom();
addPresent();
addNotPresent();
presentButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
presentLayout.setVisibility(View.VISIBLE);
notPresentLayout.setVisibility(View.GONE);
}
});
notPresentButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
notPresentLayout.setVisibility(View.VISIBLE);
presentLayout.setVisibility(View.GONE);
}
});
}
private void addBottom(){
list = new ArrayList<GridInfo>();
list.add(new GridInfo("全部禁言"));
list.add(new GridInfo("全部发言"));
list.add(new GridInfo("录音"));
list.add(new GridInfo("追加参会"));
list.add(new GridInfo("结束会议"));
bottomAdapter = new GridBottomAdapter(this);
bottomAdapter.setList(list);
gridBottom.setNumColumns(5);
gridBottom.setAdapter(bottomAdapter);
}
private void addNotPresent() {
list = new ArrayList<GridInfo>();
list.add(new GridInfo("张三","123456789","呼叫中"));
list.add(new GridInfo("李四","124678066","未接通"));
notPresentAdapter = new GridNotPresentAdapter(this);
notPresentAdapter.setList(list);
notPresentGrid.setNumColumns(1);
notPresentGrid.setAdapter(notPresentAdapter);
}
private void addPresent() {
// TODO Auto-generated method stub
list = new ArrayList<GridInfo>();
list.add(new GridInfo("张三","123456789"));
list.add(new GridInfo("李四","124678066"));
presentAdapter = new GridPresentAdapter(this);
presentAdapter.setList(list);
presentGrid.setNumColumns(1);
presentGrid.setAdapter(presentAdapter);
}
}