我正在尝试动态创建一个布局,我遇到了一个小问题。出于某种原因,我无法弄清楚如何设置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?
答案 0 :(得分:2)
LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, .60f);
最后一个参数是权重(浮点数),而LinearLayout.LayoutParams将修复导入错误的layoutparams时出现的错误