1. 程式人生 > >Android 動態設定佈局屬性

Android 動態設定佈局屬性

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 …
選擇的時候主要是根據其父佈局來選擇.