android代码中动态添加控件

package cn.shitou.demo.scroollistview;

import java.util.ArrayList;
import java.util.List;

今晚花时间研究,简单实现了一下在Android代码中动态添加控件的功能

 

MainActivity代码:

public class MainActivity extends Activity {

 private ListView listView;
 private List<String> listData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }
 private void initView() {
  listView = (ListView) findViewById(R.id.listView);
 }
 
 private void initData() {
  listData = new ArrayList<String>();
  for(int i=0; i<100; i++){
   listData.add("我是第"+i);
  }
  MyAdapter adapter = new MyAdapter();
  listView.setAdapter(adapter);
 }
 private class MyAdapter extends BaseAdapter{

  @Override
  public int getCount() {
   return listData.size();
  }

  @Override
  public Object getItem(int position) {
   return null;
  }

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

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   ViewHolder holder;
   if(convertView == null){
    convertView = View.inflate(MainActivity.this, R.layout.item_listview, null);
    holder = new ViewHolder();
    holder.btn = (Button) convertView.findViewById(R.id.btn_text);
    holder.ll_btn = (LinearLayout) convertView.findViewById(R.id.ll_btn);
    convertView.setTag(holder);
   }else{
    holder = (ViewHolder) convertView.getTag();
   }
   
   /**动态添加第一个测试按钮**/
   Button btn1 = new Button(MainActivity.this);
   btn1.setText("测试按钮1");
   //把30dp根据对应的硬件设备像素密度转化为对应的px值
   int pxWidth = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 140, getResources().getDisplayMetrics());
   int pxHeight = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
   //设置btn1的宽和高
   btn1.setLayoutParams(new LinearLayout.LayoutParams(pxWidth, pxHeight));
   holder.ll_btn.addView(btn1);
   
   /**动态添加第二个测试按钮**/
   Button btn2 = new Button(MainActivity.this);
   btn2.setText("测试按钮2");
   //设置btn2的宽和高
   btn2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
   holder.ll_btn.addView(btn2);
   
   /**动态添加第三个按钮**/
   Button btn3 = new Button(MainActivity.this);
   btn3.setText("测试按钮3");
   //设置btn3的宽和高
   btn3.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
   btn3.setBackgroundResource(R.drawable.ic_launcher);
   holder.ll_btn.addView(btn3);
   
   holder.btn.setText(listData.get(position));
   
   
   return convertView;
  }
  
  private class ViewHolder{
   private Button btn;
   private LinearLayout ll_btn;
  }
  
 }
 
 
}

对应的XML    activity_main布局文件代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>

对应的XML Item_listview代码

<?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="vertical" >

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="@null" >

        <LinearLayout
            android:id="@+id/ll_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btn_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher"
                android:text="第一" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher"
                android:text="第二" />

        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

 

 

 

并且实现了listView横向滑动效果。收工。之后会继续完善的。


 

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/abc6368765/article/details/50465238
个人分类: Android技术
上一篇android布局属性详解
下一篇HorizontalScrollView嵌套GridView实现横向滑动
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭