Как сделать два разных макета, чтобы иметь одинаковую высоту? - PullRequest
1 голос
/ 28 ноября 2011

В моем XML-макете приложения у меня есть один Relativelayout, в котором есть два линейных макета. Сейчас я делаю анимацию для этого макета. Но из-за того, что высота кажется разной, у меня возникла проблема со зрением.

Итак, я хочу сделать одинаковую высоту обоих макетов. Ниже мой XML-файл.

<!-- ============================================================= -->
            <!-- BOTTLE / PEN LAYOUT -->
            <!-- ============================================================= -->
            <LinearLayout android:layout_height="wrap_content"
                android:layout_width="fill_parent" >

                <RelativeLayout android:layout_height="wrap_content"
                    android:layout_width="fill_parent">
                    <!-- ============================================================= -->
                    <!-- PEN LAYOUT -->
                    <!-- ============================================================= -->
                    <LinearLayout android:orientation="vertical"
                        android:layout_height="wrap_content"
                        android:layout_width="fill_parent"
                        android:id="@+id/pen_layout">

                        <TextView
                            android:text="EF"
                            android:layout_height="wrap_content"
                            android:layout_width="wrap_content"
                            android:layout_gravity="center"
                            android:textColor="#000000"
                            android:layout_marginTop="2dp"
                            android:textSize="10sp"/>

                        <ImageView                 
                            android:id="@+id/save"                 
                            android:layout_width="wrap_content"                 
                            android:layout_height="wrap_content"
                            android:layout_gravity="right"   
                            android:src="@drawable/ink_pen"/>             

                        <TextView
                            android:text="F"
                            android:layout_height="wrap_content"
                            android:layout_width="wrap_content"
                            android:layout_gravity="center"
                            android:textColor="#000000"
                            android:layout_marginTop="2dp"
                            android:textSize="10sp"/>
                        <ImageView                 
                            android:id="@+id/save"                 
                            android:layout_width="wrap_content"                 
                            android:layout_height="wrap_content"
                            android:layout_gravity="right"   
                            android:src="@drawable/ink_pen"/>             

                        <TextView
                            android:text="BOLD"
                            android:layout_height="wrap_content"
                            android:layout_width="wrap_content"
                            android:layout_gravity="center"
                            android:textColor="#000000"
                            android:layout_marginTop="2dp"
                            android:textSize="10sp"/>
                        <ImageView                 
                            android:id="@+id/save"                 
                            android:layout_width="wrap_content"                 
                            android:layout_height="wrap_content"
                            android:layout_gravity="right"   
                            android:src="@drawable/ink_pen"/>             

                        <TextView
                            android:text="ITALIC"
                            android:layout_height="wrap_content"
                            android:layout_width="wrap_content"
                            android:layout_gravity="center"
                            android:textColor="#000000"
                            android:layout_marginTop="2dp"
                            android:textSize="10sp"/>
                        <ImageView                 
                            android:id="@+id/save"                 
                            android:layout_width="wrap_content"                 
                            android:layout_height="wrap_content"
                            android:layout_gravity="right"   
                            android:src="@drawable/ink_pen"/>

                    </LinearLayout> 

                    <!-- ============================================================= -->
                    <!-- BOTTLE LAYOUT -->
                    <!-- ============================================================= -->

                    <LinearLayout android:orientation="vertical"
                        android:layout_height="wrap_content"
                        android:layout_width="fill_parent"
                        android:weightSum="4" 
                        android:id="@+id/bottle_layout">

                        <!-- First Row Bottle -->
                        <LinearLayout android:orientation="horizontal"
                            android:layout_height="wrap_content" android:layout_weight="1"
                            android:layout_width="fill_parent" android:weightSum="2">
                            <ImageView android:layout_height="wrap_content" android:layout_weight="1"
                                android:layout_width="wrap_content" android:src="@drawable/purple_bottle"/>
                            <ImageView android:layout_height="wrap_content" android:layout_weight="1"
                                android:layout_width="wrap_content" android:src="@drawable/red_bottle"/>
                        </LinearLayout>

                        <!-- Second Row Bottle -->              
                        <LinearLayout android:orientation="horizontal"
                            android:layout_height="wrap_content" android:layout_weight="1"
                            android:layout_width="fill_parent" android:weightSum="2">
                            <ImageView android:layout_height="wrap_content" android:layout_weight="1"
                                android:layout_width="wrap_content" android:src="@drawable/gray_bottle"/>
                            <ImageView android:layout_height="wrap_content" android:layout_weight="1"
                                android:layout_width="wrap_content" android:src="@drawable/green_bottle"/>
                        </LinearLayout>

                        <!-- Third Row Bottle -->               
                        <LinearLayout android:orientation="horizontal"
                            android:layout_height="wrap_content" android:layout_weight="1"
                                android:layout_width="fill_parent" android:weightSum="2">
                            <ImageView android:layout_height="wrap_content" android:layout_weight="1"
                                android:layout_width="wrap_content" android:src="@drawable/orange_bottle"/>
                            <ImageView android:layout_height="wrap_content" android:layout_weight="1"
                                android:layout_width="wrap_content" android:src="@drawable/blue_bottle"/>
                        </LinearLayout>

                        <!-- Forth Row Bottle -->               
                        <LinearLayout android:orientation="horizontal"
                            android:layout_height="wrap_content" android:layout_weight="1"
                            android:layout_width="fill_parent" android:weightSum="2">
                            <ImageView android:layout_height="wrap_content" android:layout_weight="1"
                                android:layout_width="wrap_content" android:src="@drawable/black_bottle"/>
                            <ImageView android:layout_height="wrap_content" android:layout_weight="1"
                                android:layout_width="wrap_content" android:src="@drawable/white_bottle"/>
                        </LinearLayout>

                    </LinearLayout>
                </RelativeLayout>
            </LinearLayout>

1 Ответ

1 голос
/ 28 ноября 2011

Удалите содержащий RelativeLayout (поскольку он является единственным дочерним элементом вашего корневого LinearLayout, он вам строго не нужен) или измените его на LinearLayout.

Затем для макета пера и макета бутылки добавьте атрибут android: layout_weight и установите для них обоих значение 1.

Убедитесь, что для обоих макетов высота установлена ​​в MATCH_PARENT

Теперь ваши две раскладки должны равномерно распределить пространство между ними, таким образом делая их одинаковой высоты.

Чтобы разделить пространство поровну между дочерними элементами раскладок Pen и Bottle, я предлагаю вам установитьвысота каждого дочернего элемента до WRAP_CONTENT, а layout_weight каждого дочернего элемента - до 1.

...