简体   繁体   中英

layout_weight programmatically explainedv

I have been looking these past two days on questions regarding setting the weight of a layout or a group of layouts programmatically.

all the answers i found are almost the same so that means i know what code no to use but i do not seem to understand how to assign the float attribute. in the following code.

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
1.0f
);
YOUR_VIEW.setLayoutParams(param);

would someone please give me an example of how to assign the weights of two TextView with a weight sum of 3???

It depends on your view if you want to split the view horizontally between them you can use something like this.

TextView secondTV = findViewById(R.id.secondTextView);
TextView firstTV = findViewById(R.id.firstTextView);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 3);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);

firstTV.setLayoutParams(layoutParams);
secondTV.setLayoutParams(layoutParams1);

And your layout looks like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/firstTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello" />

    <TextView
        android:id="@+id/secondTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/icon"
        android:text="Hello" />

</LinearLayout>

but if you want to split the view vertically you can use something like this.

TextView secondTV = findViewById(R.id.secondTextView);
TextView firstTV = findViewById(R.id.firstTextView);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 3);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);

firstTV.setLayoutParams(layoutParams);
secondTV.setLayoutParams(layoutParams1);

And your layout looks like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/firstTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello" />

    <TextView
        android:id="@+id/secondTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/icon"
        android:text="Hello" />

</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM