我有使用relativelayout的av xml文件,它工作正常,现在我希望它转换为程序化形式,我的xml是..
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/a"
android:layout_alignParentLeft="true"
android:text="" />
<TextView
android:id="@+id/d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_toRightOf="@+id/b"/>
<TextView
android:id="@+id/c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_below="@+id/b"/>
<TextView
android:id="@+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/d"
android:layout_below="@+id/d"
android:text="" />
<TextView
android:id="@+id/e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_below="@+id/c"/>
<TextView
android:id="@+id/f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/e"
android:layout_alignBottom="@+id/e"
android:layout_alignLeft="@+id/a"
android:text="" />
</RelativeLayout>
请给我一些想法如何将其转换为程序化..我试图转换为以程序化形式编写,但我不知道addrule ..指导我..
答案 0 :(得分:2)
按照以下代码添加规则:
LinearLayout linearLayout = new LinearLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
parentView.addView(linearLayout, layoutParams);
答案 1 :(得分:1)
请尝试这种方式,希望这有助于您解决问题。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(layoutParams);
TextView textViewB = new TextView(this);
textViewB.setId(1);
textViewB.setText("b");
RelativeLayout.LayoutParams layoutParamsB = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParamsB.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParamsB.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textViewB.setLayoutParams(layoutParamsB);
TextView textViewD = new TextView(this);
textViewD.setId(2);
textViewD.setText("d");
RelativeLayout.LayoutParams layoutParamsD = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParamsB.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParamsD.addRule(RelativeLayout.RIGHT_OF,textViewB.getId());
textViewD.setLayoutParams(layoutParamsD);
TextView textViewC = new TextView(this);
textViewC.setId(3);
textViewC.setText("c");
RelativeLayout.LayoutParams layoutParamsC = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParamsC.addRule(RelativeLayout.BELOW,textViewB.getId());
layoutParamsC.addRule(RelativeLayout.ALIGN_LEFT);
textViewC.setLayoutParams(layoutParamsC);
TextView textViewA = new TextView(this);
textViewA.setId(4);
textViewA.setText("a");
RelativeLayout.LayoutParams layoutParamsA = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParamsA.addRule(RelativeLayout.BELOW,textViewD.getId());
layoutParamsA.addRule(RelativeLayout.RIGHT_OF,textViewC.getId());
textViewA.setLayoutParams(layoutParamsA);
TextView textViewE = new TextView(this);
textViewE.setId(5);
textViewE.setText("e");
RelativeLayout.LayoutParams layoutParamsE = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParamsE.addRule(RelativeLayout.BELOW,textViewC.getId());
layoutParamsE.addRule(RelativeLayout.ALIGN_LEFT);
textViewE.setLayoutParams(layoutParamsE);
TextView textViewF = new TextView(this);
textViewF.setId(6);
textViewF.setText("f");
RelativeLayout.LayoutParams layoutParamsF = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParamsF.addRule(RelativeLayout.BELOW,textViewA.getId());
layoutParamsF.addRule(RelativeLayout.RIGHT_OF,textViewE.getId());
textViewF.setLayoutParams(layoutParamsF);
relativeLayout.addView(textViewB);
relativeLayout.addView(textViewD);
relativeLayout.addView(textViewC);
relativeLayout.addView(textViewA);
relativeLayout.addView(textViewE);
relativeLayout.addView(textViewF);
setContentView(relativeLayout);
}
答案 2 :(得分:0)
仅供参考,您不应该(并且您不被允许)询问任何工具或资源。
但是,我知道一个工具可以用来为你的XML布局生成Java代码,Android Layout Finder。在这个Web工具中,您只需要粘贴XML,选择适当的选项,它将为您提供Java代码。