Android development sets various properties of linear layout and relative layout RelativeLayout in the code

Keywords: Android xml Java Asterisk

1. In the first case, I do not define the xml layout file at all. The layout file is completely written in the Java file. The code is as follows:

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Parent layout for all controls
        RelativeLayout relativeLayout=new RelativeLayout(this);
        //Set the width and height of the parent layout
        relativeLayout.setLayoutParams(new RelativeLayout
                .LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        //new to add
        TextView textView1=new TextView(this);
        //Set its id, font and color
        textView1.setId(1);
        textView1.setTextColor(Color.BLACK);
        textView1.setTextSize(20);
        textView1.setText("This is new The first one textView1");
        //Set the width and height of TextView1
        RelativeLayout.LayoutParams layoutParams=new RelativeLayout
                .LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        //Set textView1 to align to the left of the parent layout. If it is LinearLayout, set layout parameters, such as align to the left of the parent layout
        //Then use the property layoutParas.gravity=Gravity.LEFT, layoutParas.setGravity(Gravity.XXX)
        // It is mainly to set the position of space content in linear layout;
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        //Set the layout property of textView1
        textView1.setLayoutParams(layoutParams);
        //Add textView1 to the parent layout
        relativeLayout.addView(textView1, layoutParams);

        //new gives the control you want
        TextView textView2=new TextView(this);
        //Set its id
        textView2.setId(2);
        textView2.setTextColor(Color.RED);
        textView2.setTextSize(18);
        textView2.setText("This is new The second one textView2");
        //Set the width and height of TextView
        RelativeLayout.LayoutParams layoutParams2=new RelativeLayout
                .LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams2.topMargin=150;
        layoutParams2.leftMargin=100;
        //Used to set position
        layoutParams2.addRule(RelativeLayout.BELOW, 1);//Set the position relative to a control
        layoutParams2.addRule(RelativeLayout.CENTER_HORIZONTAL);//Position in parent
        relativeLayout.addView(textView2, layoutParams2);
        setContentView(relativeLayout);
    }

}

2. If there is already a parent layout, you only want to set the corresponding parameters of the controls in the parent layout. If I customize a dialog box, the content of the dialog box is in the form of a list, and I customize the layout of list item s, as follows:

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

    <TextView
        android:id="@+id/star"
        android:layout_width="4dp"
        android:layout_height="4dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="5dp"
        android:background="@drawable/round_point" />

    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="7dp"
        android:layout_toRightOf="@id/star"
        android:lineSpacingExtra="10px"
        android:singleLine="false"
        android:text="If you fail to confirm the payment within 30 minutes after receiving the fare bill, wechat payment will automatically deduct the payment and notify you by SMS"
        android:textColor="@color/text_color_ss_gray"
        android:textSize="@dimen/font_size_xx_medium" />

</merge>

Then define a DialogListItem class that inherits from RelativeLayout:

public class DialogListItem extends RelativeLayout {
    /**Asterisk displayed**/
    private TextView star;
    /**Copy displayed**/
    private TextView content;

    public DialogListItem(Context context) {
        this(context, null);
    }

    public DialogListItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DialogListItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initViews(context);
    }

    private void initViews(Context context) {
        LayoutInflater.from(context).inflate(R.layout.dialog_list_item, this);
        star = (TextView) findViewById(R.id.star);
        content = (TextView) findViewById(R.id.content);
    }

    public void setContent(String content) {
        this.content.setText(content);
    }

    public void setSpannedContent(Spanned content) {
        this.content.setText(content);
    }

    public void setStarInvisible() {
        star.setVisibility(INVISIBLE);
    }

}

Finally, I started to define mydialog to inherit Dialog. Setting the Dialog content in mydialog's xml layout file can make the list or not, but the title and OK Cancel buttons are to be displayed in the middle, which leads to the Dialog content also to be displayed in the middle, as shown in the following code. The parent layout has set the property of Android: gravity = "center? Horizontal", which leads to LinearLayout All controls in the layout should be centered:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="285dp"
    android:layout_height="wrap_content"
    android:background="@drawable/common_white_bg_round_cornor_bg"
    android:gravity="center_horizontal"
    android:paddingTop="20dp"
    android:baselineAligned="false"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/dialogIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_centerHorizontal="true"
            android:src="@drawable/dialog_icon_exclam" />
        <TextView
            android:id="@+id/ivText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:layout_centerHorizontal="true"
            android:text="13345676789"
            android:textColor="@color/text_color_white"
            android:textSize="@dimen/font_size_xx_small"
            android:visibility="gone" />
    </RelativeLayout>

    <TextView
        android:id="@+id/dialogTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="22dp"
        android:paddingRight="22dp"
        android:gravity="center_horizontal"
        android:textColor="@color/text_color_dark_gray"
        android:textSize="@dimen/font_size_xxxx_large" />

    <TextView
        android:id="@+id/dialogContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lineSpacingExtra="7dp"
        android:paddingLeft="22dp"
        android:paddingRight="22dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:textColor="@color/text_color_ss_gray"
        android:textSize="@dimen/font_size_xx_medium" />
    <LinearLayout
        android:id="@+id/dialogContentListLayout"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lineSpacingExtra="7dp"
        android:paddingLeft="22dp"
        android:paddingRight="22dp"
        android:visibility="gone"
        android:orientation="vertical"/>

    <LinearLayout
        android:id="@+id/dialogContentCheckboxLayout"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lineSpacingExtra="7dp"
        android:paddingLeft="22dp"
        android:paddingRight="22dp"
        android:layout_gravity="left"
        android:visibility="gone"
        android:orientation="vertical">

        <CheckBox
            android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/next_time_no_remind"
            android:textColor="@color/afanty_light_gray"
            android:button="@null"
            android:background="@null"
            android:drawableLeft="@drawable/round_radio_selector"
            android:drawablePadding="5dp"
            />
        </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="20dp"
        android:background="@color/line_color_gray" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <Button
            android:id="@+id/dialogBtnOne"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:drawableRight="@drawable/common_inner_vertical_line"
            android:gravity="center"
            android:clickable="true"
            android:textColor="@color/common_dark_blue"
            android:text="@string/cancel"
            android:textSize="@dimen/font_size_xxx_large" />

        <Button
            android:id="@+id/dialogBtnTwo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:gravity="center"
            android:text="@string/dialog_good_txt"
            android:clickable="true"
            android:textColor="@color/common_blue_btn_trigger_normal"
            android:textSize="@dimen/font_size_xxx_large" />
    </LinearLayout>
</LinearLayout>

So how can we change the layout parameters of this single control that is not centered?

So we should do this in the MyDialog dialog dialog:

/**
This function is mainly used to set the title, content, confirm cancel and other contents in the dialog box. As for the display dialog box, you need to use the show method
     * @param dialogIconResId   Pass - 1 means this item needs to be hidden
     * @param dialogTitleTxt    Passing null means that this item needs to be hidden
     * @param dialogContentList Passing null means that this item needs to be hidden
     * @param dialogBtnOneTxt   Passing null means this item will not be displayed
     * @param dialogBtnTwoTxt   Cannot be null
     * @return
     */
    public CommonDialog set(List<String> dialogContentList, int dialogIconResId, String dialogTitleTxt, String dialogBtnOneTxt, String dialogBtnTwoTxt) {
        if (dialogIconResId < 0) {
            dialogIcon.setVisibility(View.GONE);
        } else {
            dialogIcon.setVisibility(View.VISIBLE);
            dialogIcon.setImageResource(dialogIconResId);
        }
        if (dialogTitleTxt == null) {
            dialogTitle.setVisibility(View.GONE);
        } else {
            dialogTitle.setVisibility(View.VISIBLE);
            dialogTitle.setText(dialogTitleTxt);
        }
        dialogContent.setVisibility(View.GONE);
        //Do not display list style when content list is empty
        if (dialogContentList == null || dialogContentList.size() == 0) {
            dialogContentListLayout.setVisibility(View.GONE);
            //When the content list length is 1, it is displayed in the form of normal content
        } else if (dialogContentList.size() == 1) {
            dialogContentListLayout.setVisibility(View.GONE);
            dialogContent.setVisibility(View.VISIBLE);
            dialogContent.setText(dialogContentList.get(0));
        } else {
            dialogContentListLayout.setVisibility(View.VISIBLE);
            dialogContentListLayout.removeAllViews();
            for (int i = 0; i < dialogContentList.size(); i++) {
                addListItem(dialogContentListLayout, dialogContentList.get(i));
            }
        }
        if (TextUtil.isEmpty(dialogBtnOneTxt)) {
            dialogBtnOne.setVisibility(View.GONE);
        } else {
            dialogBtnOne.setVisibility(View.VISIBLE);
            dialogBtnOne.setText(dialogBtnOneTxt);
        }
        dialogBtnTwo.setText(dialogBtnTwoTxt);
        return this;
    }

The function addListItem is the focus of this explanation, focusing on the following:

  private void addListItem(LinearLayout parent, String item) {
        DialogListItem itemView = new DialogListItem(mContext);
        //RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //lp.topMargin = ResourcesUtil.getDimensionPixelSize(R.dimen.dialog_list_item_margin);
        //Set the contents of the dialog list to be aligned to the left, 10dp above
        LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.gravity=Gravity.LEFT;
        lp.topMargin=ResourcesUtil.getDimensionPixelSize(R.dimen.dialog_list_item_margin);
        itemView.setContent(item);
        //itemView.setLayoutParams(lp);
        parent.setLayoutParams(lp);
//        itemView.setPadding(60, 0, 86, 30);
        parent.addView(itemView);
    }

Here we use LinearLayout.LayoutParams lp=new LinearLayout. Layoutparams (ViewGroup. Layoutparams. Wrap? Content, ViewGroup. Layoutparams. Wrap? Content);
lp.gravity=Gravity.LEFT;

To set the left alignment of the dialog content layout relative to the parent layout.

Posted by Cateyes on Sun, 01 Dec 2019 13:50:18 -0800