开发者

Android Programatically add three Relative Layouts within a single Linear Layout

I have a layout like this in xml format. But I need to do all these things programatically in java code. Means the whole xml file in java code. Can any body help me on this. I am new to android

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:id="@+id/layoutTop"

        >
        <Button android:id="@+id/btnBack" 
                android:text="Back"
                android:layout_width="100dp"
                android:layout_height="40dp"
                android:layout_marginTop="7dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="2dp" 
                android:textColor="#000000"

            /> 
        <ImageView 
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="40dp" 
            android:src="@+drawable/logo"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="7dp"

        />

        <Button android:id="@+id/btnQuit" 
                android:text="Quit"
                android:layout_width="100dp"
                android:layout_height="40dp"
                android:layout_marginTop="7dp"
                android:textColor="#000000"
                android:layout_alignParentRight="true"
            />  

    </RelativeLayout>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="370dp"
        android:id="@+id/layoutMiddle"
        android:layout_below="@+id/layoutTop"
        android:background="#ffffff"
        android:layout_marginRight="2dp"
        android:layout_marginLeft="2dp" 
        >

        <TextView 
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:textColor="#000000"
            android:textSize="15dp"
            android:textStyle="bold"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
        />
    </RelativeLayout>
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_below="@+id/layoutMiddle"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"
        >

    <!-- 开发者_运维百科<ProgressBar
        android:layout_width="220dp" 
        android:layout_height="20dip" 
        style="?android:attr/progressBarStyleHorizontal"
        android:progressDrawable="@drawable/progress_bar_states"
        android:id="@+id/progressbar_Horizontal"
        android:max="100"
        android:layout_marginTop="7dp"
        android:layout_marginLeft="2dp"
        android:indeterminateOnly="false" 
        /> -->

        <SeekBar
            android:layout_width="220dp" 
            android:layout_height="30dip" 
            style="?android:attr/progressBarStyleHorizontal"
            android:progressDrawable="@drawable/progress_bar_states"
            android:id="@+id/progressbar_Horizontal"
            android:max="100"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="2dp"
            android:indeterminateOnly="false" 
        /> 

    <Button android:id="@+id/btnSend" 
                android:text="Send"
                android:layout_width="88dp"
                android:layout_height="40dp"
                android:layout_marginTop="2dp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="2dp"    
                android:textColor="#000000"
                android:layout_alignParentBottom="true"
                /> 


     </RelativeLayout>

</LinearLayout>


You can for xml file because it is faster.But if you need then you can do this:

LinearLayout parent=new LinearLayout(this);
RelativeLayout rl1=new RelativeLayout(this);
RelativeLayout rl2=new RelativeLayout(this);
RelativeLayout rl3=new RelativeLayout(this);

You can add buttons and imageview and all.Then add

rl1.addView(bt1);
.......
rl2.addView(img1);
....
rl3.addView(bt2);
.......
parent.addView(rl1);
parent.addView(rl2);
parent.addView(rl3);


You should really check this blogpost about doing layouts programatically. Then you should be able to do it by yourself really fast.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜