如何用java代码创建relativelayouts?

时间:2012-03-07 12:02:18

标签: android relativelayout

我需要学习使用java代码而不是XML代码创建relativelayouts。

例如,很高兴了解如何使用java代码构建此XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 <Button
        android:id="@+id/btnButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"/>
<Button
        android:id="@+id/btnButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"
        android:layout_toRightOf="@+id/btnButton1"/> 
     <Button
        android:id="@+id/btnButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3"
        android:layout_below="@+id/btnButton1"/>
</RelativeLayout>

我找不到任何使用java代码执行此操作的示例

请问有人可以帮助我将该布局转换为java代码吗?或者有人可以给我教程链接用java代码做相对布局吗?

感谢

3 个答案:

答案 0 :(得分:2)

那里有很多例子!试试这个教程,它涵盖了你需要的所有元素......最好的是,它以编程方式为所有类型的布局提供示例,因此它也将解决未来的问题;)

http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-relative-layouts/

答案 1 :(得分:1)

我希望此代码可以帮助您以编程方式实现relativelayout

RelativeLayout.LayoutParams expandLayoutParams = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
expandLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
expandLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
expandLayoutParams.topMargin=7;
expandLayoutParams.rightMargin=7;
expandButton.setLayoutParams(expandLayoutParams);

还有一件事。您放置的代码我不认为代表您要实现的接口。为了让你想要,我认为最好使用linearlayout,其方向等于水平,所有按钮的layout_width = 0dip和layout_weight = 1

答案 2 :(得分:1)

    //you create relative layout dynamically like below:
public class TwoPicksOnEachOther extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Grabbing the Application context         
        final Context context = getApplication();                   

        RelativeLayout relativeLayout = new RelativeLayout(this);                   

        final ImageView iv = new ImageView(this);         
        iv.setImageResource(R.drawable.fish2);


        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(     
                RelativeLayout.LayoutParams.FILL_PARENT, 
                RelativeLayout.LayoutParams.FILL_PARENT);
        relativeLayout.addView(iv,lp);        

        // Creating transparent image
        final ImageView iv2 = new ImageView(this);
        iv.setImageResource(R.drawable.ctdeasytwo);
        RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(     
                RelativeLayout.LayoutParams.FILL_PARENT, 
                RelativeLayout.LayoutParams.FILL_PARENT);
        relativeLayout.addView(iv2,lp2);
        setContentView(relativeLayout);

    }        

}