我正在尝试动态创建布局,但遇到了一个小问题。由于某种原因,我只是想不出如何设置ImageViews的权重。我在一个教程网站上查询了此信息,但是当我尝试使用它时出现错误:

LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1);

我要完成的工作是(从“概述”角度看):

--LinearLayout(水平)(LL1)

------ ImageView(IV1)

------ LinearLayout(垂直)(LL2)

我试图用XML完成的代码:

        <LinearLayout
        android:id="@+id/skillOneAbility"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/skillOneImage"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>


        <LinearLayout
            android:id="@+id/skillOneAbilityInfo"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:orientation="vertical" >


这是我到目前为止的代码,但是我不确定如何设置ImageView的权重:

 private LinearLayout createInnerHorizontalLayout1() {
    LinearLayout mainLayout = new LinearLayout(getActivity());
    mainLayout.setOrientation(LinearLayout.HORIZONTAL);
    mainLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    //Create the image view and the linear layout with text views
    ImageView iView = new ImageView(getActivity());
    iView.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT));

    LinearLayout myInnerVerticalLayout = createInnerVerticalLayout();
}


我将如何完成链接到顶部的XML?

最佳答案

LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, .60f);


最后一个参数是权重(浮点数),使用LinearLayout.LayoutParams将解决导入错误的layoutparams时出现的错误

07-28 03:34
查看更多