我的xml文件中有以下布局。我的xml中还有其他类似的RelativeLayouts

    <RelativeLayout
    android:id="@+id/contentTextViewLayout"
    android:layout_weight="0.9"
    android:layout_width="fill_parent"
    android:layout_height="0dip">
    <TextView
        android:id="@+id/textViewStory1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:lineSpacingMultiplier="1.2"
        android:textColor="#000000"
        android:background="@drawable/book"/>
</RelativeLayout>


我想在代码中更改layout_weightRelativeLayout参数。我怎样才能做到这一点?

最佳答案

在这里查看关于第一个问题的评论:

Set the layout weight of a TextView programmatically

将您的relativelayout替换为textview。

编辑,将其全部拼出:

首先为您的外部LinearLayout充气:

    LayoutInflater inflater = LayoutInflater.from(context);
    LinearLayout ll = (LinearLayout)
        inflater.inflate(R.layout.some_linear_layout, null);
    // now get a reference to your relativelayout inside it
    RelativeLayout yourRL = (RelativeLayout)
        ll.findViewById(that_relative_layout);

    // parameters are width, height, weight
    yourRL.setLayoutParams(new
        LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, 1f));

10-07 19:24
查看更多