Android 动态设置布局属性

本文介绍如何在Android应用中动态添加TextView和ImageView等View组件到不同的布局容器中,包括LinearLayout和RelativeLayout,并提供了详细的代码示例。

Android在XML文件中写布局很方便, 但有时候不够灵活, 有时候我们需要动态添加View或者ViewGroup.
点击动态添加TextView:
这里写图片描述

 private LinearLayout mLinearLayout;
    private int i = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLinearLayout = (LinearLayout) findViewById(R.id.container);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                TextView textView = new TextView(MainActivity.this);
                LinearLayout.LayoutParams tvLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                textView.setLayoutParams(tvLayoutParams);
                textView.setText("动态添加的TextView" + "--" + i++);
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20.0F);
                textView.setGravity(Gravity.CENTER);
                mLinearLayout.addView(textView);
            }
        });
    }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@drawable/divider_shape"
    android:orientation="vertical"
    android:showDividers="middle|end">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="添加" />

</LinearLayout>

divider_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <size android:height="1dp" />
    <solid android:color="@color/colorAccent" />
</shape>
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout linearLayout = new LinearLayout(this);
        //LayoutParams有多个, 注意看情况使用
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(layoutParams);
        linearLayout.setBackgroundColor(Color.GRAY);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setGravity(Gravity.CENTER_VERTICAL);
        TextView textView = new TextView(this);
        LinearLayout.LayoutParams tvLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        tvLayoutParams.weight = 1; //LinearLayout.LayoutParams 里有weight属性
        textView.setLayoutParams(tvLayoutParams);
        textView.setText("动态添加的TextView");
        //第一个参数可以指定单位, 如 TypedValue.COMPLEX_UNIT_SP代表SP
        //TypedValue.COMPLEX_UNIT_DIP 代表DP
        //TypedValue.COMPLEX_UNIT_PX 代表PX
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20.0F);
        textView.setTextColor(Color.RED);
        textView.setGravity(Gravity.CENTER);
        textView.setBackgroundColor(Color.GREEN);
        ImageView imageView = new ImageView(this);
        LinearLayout.LayoutParams imageLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        imageLayoutParams.leftMargin = 100;
        imageLayoutParams.rightMargin = 100;
        imageView.setLayoutParams(imageLayoutParams);
        imageView.setImageResource(R.mipmap.ic_launcher);
        linearLayout.addView(textView);//添加到ViewGroup中
        linearLayout.addView(imageView);//添加到ViewGroup中
        setContentView(linearLayout);

这里写图片描述

RelativeLayout的

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RelativeLayout relativeLayout = new RelativeLayout(this);
        //LayoutParams有多个, 注意看情况使用
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        relativeLayout.setLayoutParams(layoutParams);
        relativeLayout.setBackgroundColor(Color.GRAY);
        TextView textView = new TextView(this);
        RelativeLayout.LayoutParams tvLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        tvLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);//addRule
        textView.setLayoutParams(tvLayoutParams);
        textView.setText("动态添加的TextView");
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20.0F);
        textView.setTextColor(Color.RED);
        textView.setGravity(Gravity.CENTER);
        textView.setBackgroundColor(Color.GREEN);
        ImageView imageView = new ImageView(this);
        RelativeLayout.LayoutParams imageLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        imageLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
        imageView.setLayoutParams(imageLayoutParams);
        imageView.setImageResource(R.mipmap.ic_launcher);
        ImageView imageView2 = new ImageView(this);
        RelativeLayout.LayoutParams imageLayoutParams2 = new RelativeLayout.LayoutParams
                (RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        imageLayoutParams2.addRule(RelativeLayout.ALIGN_PARENT_END);
        imageLayoutParams2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        imageView2.setLayoutParams(imageLayoutParams2);
        imageView2.setImageResource(R.mipmap.ic_launcher);
        relativeLayout.addView(textView);
        relativeLayout.addView(imageView);
        relativeLayout.addView(imageView2);
        setContentView(relativeLayout);
    }

这里写图片描述

addRule支持如下属性

这里写图片描述

LayoutParams

可以看到, LayoutParams 前面有很多:
RelativeLayout.LayoutParams
LinearLayout.LayoutParams
ViewGroup.LayoutParams …
选择的时候主要是根据其父布局来选择.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值