简体   繁体   中英

Multiple layouts, one activity

I want to create an activity that contains 5 " FrameLayout " like the picture below. 3 equal FrameLayout in the first row and 2 FrameLayout in the second row.

在此处输入图片说明

I did it using Linear layouts but I got a warning that nested weights are bad . So is there an other way to make it.

Fragments should be streched on the screen without using values like (100dp).

Try this

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_weight="1"
                android:background="#00bbff" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="1" />
            </FrameLayout>

            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_weight="1"
                 android:background="#bb00ff" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="2" />
            </FrameLayout>

            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_weight="1"
                 android:background="#00ff00" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="3" />
            </FrameLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_weight="1" 
                 android:background="#bbff00">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="4" />
            </FrameLayout>

            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_weight="1"
                 android:background="#ffbb00" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="5" />
            </FrameLayout>
        </LinearLayout>

    </LinearLayout>

you cal also use the two table layout.. like

<TableLayout android:weight='1'>
   <TableRow>
      <Linear> 
        <FrameLayout /> <FrameLayout /> <FrameLayout /> 
      </Linear>
   </TableRow>
</TableLayout>

<TableLayout android:weight='1'>
   <TableRow>
      <Linear> 
        <FrameLayout /> <FrameLayout/>
      </Linear>
   </TableRow>
</TableLayout>

its rough layout . i hope you got the idea.

Try this::Hope its useful for you.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp" >

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:background="#00bbff"
              >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="1" />
        </FrameLayout>


        <View  android:layout_width="10dp"
        android:layout_height="10dp"
        />

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:background="#bb00ff" 
           >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="2" 
                />
        </FrameLayout>

          <View  android:layout_width="10dp"
        android:layout_height="10dp"
        />
        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:background="#00ff00" 
           >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="3" />
        </FrameLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
         android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp">

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:background="#bbff00" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="4" />
        </FrameLayout>

         <View  android:layout_width="10dp"
        android:layout_height="10dp"
        />

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:background="#ffbb00" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="5" />
        </FrameLayout>
    </LinearLayout>

</LinearLayout>
  see image below  ![image][1]

See This code
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> 
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"> 
    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#FFFFFF"
        android:gravity="center"
        android:orientation="vertical"> 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="1"/>
    </LinearLayout> 
    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginLeft="2dp"
        android:background="#FFFFFF"
        android:gravity="center"
        android:orientation="vertical"> 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="1"/>
    </LinearLayout> 
    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginLeft="2dp"
        android:background="#FFFFFF"
        android:gravity="center"
        android:orientation="vertical"> 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="1"/>
    </LinearLayout>
</LinearLayout> 
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:layout_marginTop="2dp"
    android:orientation="horizontal"> 
    <LinearLayout
        android:layout_width="150dp"
        android:layout_height="fill_parent"
        android:background="#FFFFFF"
        android:gravity="center"
        android:orientation="vertical"> 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"/>
    </LinearLayout> 
    <LinearLayout
        android:layout_width="150dp"
        android:layout_height="fill_parent"
        android:background="#FFFFFF"
        android:gravity="center"
        android:layout_marginLeft="1dp"
        android:orientation="vertical"> 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4" />
    </LinearLayout>
</LinearLayout> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM