Android开发---动态加载控件

228 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

目标:通过后端实现动态加载控件

1.主页面先修改成线性布局

image.png

2.绘制主页面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="张三"
            ></TextView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="13628682141"
            ></TextView>
    </LinearLayout>
</LinearLayout>

3.主页面在设置一下排布方式: 1)当在线性布局中LinearLayout设置了android:orientation="horizontal"时,表示此时的排列方式为水平方向 2)当在线性布局中LinearLayout设置了android:orientation="vertical"时,表示此时的排列方式为垂直方向

目前的效果图:

image.png

4.那么如何可以显示不同的人名以及对应的手机号码呢,并且成垂直排布? 可以在主页面(activity_main.xml)写不同的LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="张三"
            ></TextView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="13628682141"
            ></TextView>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="李四"
            ></TextView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="13628682141"
            ></TextView>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="王五"
            ></TextView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="13628682141"
            ></TextView>
    </LinearLayout>
</LinearLayout>

目前的效果图为:

image.png

但是这样写太过于麻烦了,如何改进呢? 5.直接在主页面(activity_main.xml)写一个线性布局即可,选择在里面设置id的方法,来实现动态加载控件的方法:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical"
       android:id="@+id/ll"
       >
   </LinearLayout>
</LinearLayout>

只需要添加一个模块即可:

image.png 6.完成主页面实现类(MainActivity.java)

public class MainActivity extends AppCompatActivity {

    private LinearLayout ll;
    private String[] name={"张三","李四","王五"};
    private String[] phone={"1783298122","1378909876","156345678"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ll=findViewById(R.id.ll);
        for (int i=0;i<3;i++){
            LinearLayout tmpll=new LinearLayout(getApplication());
            LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            tmpll.setOrientation(LinearLayout.HORIZONTAL);

            TextView tv_name=new TextView(this);
            tv_name.setText(name[i]);
            tmpll.addView(tv_name);

            TextView tv_phone=new TextView(this);
            tv_phone.setText(phone[i]);
            tmpll.addView(tv_phone);

            ll.addView(tmpll);
        }
    }
}

最后的实现效果图:

image.png

总结:通过主页面设置id的方法,主要功能实现在后端实现类来完成即可!