Java 以编程方式解释的布局重量

Java 以编程方式解释的布局重量,java,android,android-layout-weight,Java,Android,Android Layout Weight,在过去的两天里,我一直在关注有关以编程方式设置布局或布局组权重的问题 我找到的所有答案几乎都是一样的,这意味着我知道应该使用什么代码no,但我似乎不知道如何分配float属性。在下面的代码中 LinearLayout.LayoutParams param = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f ); YOUR_VIEW.setLayoutParams(p

在过去的两天里,我一直在关注有关以编程方式设置布局或布局组权重的问题

我找到的所有答案几乎都是一样的,这意味着我知道应该使用什么代码no,但我似乎不知道如何分配float属性。在下面的代码中

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

有人能给我举个例子,说明如何分配权重和为3的两个文本视图的权重吗?

这取决于您的视图如果您想在它们之间水平分割视图,您可以使用类似的方法

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);
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);
你的布局是这样的

<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>
<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>
你的布局是这样的

<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>
<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>

你想用这种方法在文本视图之间分割视图吗?是的,两个不同的文本视图,我想控制它们在一起的外观:一个比另一个大。是的,它是4,但你可以使用这个数字,并根据需要设置。