简体   繁体   中英

Move LinearLayout to bottom of parent LinearLayout

I have a LinearLayout that has a background image.

The LinearLayout also has a child LinearLayout with a TextView inside it. The child LinearLayout has a 20 % transparent background color.

My problem is that I want to move the child LinearLayout to the bottom of the parent LinearLayout. Now the child LinearLayout is located in the vertical center of the parent LinearLayout.

Image:

在此处输入图片说明

I have tried different techniques, including using RelativeLayout.

The design XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fitsSystemWindows="true"
    android:fillViewport="true"
    tools:context=".MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <!-- Main season image -->
        <LinearLayout
            android:id="@+id/linearLayoutMain"
            android:layout_width="match_parent"
            android:layout_height="300px"
            android:orientation="vertical"
            android:background="@drawable/img_season_02">
        </LinearLayout>
        <!-- //Main season image -->

        <!-- Navigation -->
        <TableLayout
            android:id="@+id/tableLayoutNavigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:columnCount="3"
            >

            <!-- Row 1 -->
            <TableRow

                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

                <!-- Row 1 - Columns 1 -->
                <LinearLayout
                    android:id="@+id/linearLayoutFood"
                    android:layout_weight=".3"
                    android:layout_width="0dp"
                    android:layout_height="100dp"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:background="@drawable/img_tile_food"
                    android:layout_marginTop="2dp"
                    android:layout_marginRight="1dp"
                    android:layout_marginLeft="2dp">


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#bb000000"
                        android:orientation="vertical"
                        android:layout_alignParentBottom="true"
                        android:paddingLeft="6dp">

                        <TextView
                            android:id="@+id/textViewMyProfileFood"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="3dp"
                            android:text="@string/food"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:textColor="@color/colorWhite" />
                    </LinearLayout>
                </LinearLayout>
                <!-- //Row 1 - Column 1 -->


                <!-- Row 1 - Column 2 -->
                <LinearLayout
                    android:id="@+id/linearLayoutExercise"
                    android:layout_width="0dp"
                    android:layout_height="100dp"
                    android:layout_marginLeft="1dp"
                    android:layout_marginRight="1dp"
                    android:layout_marginTop="2dp"
                    android:layout_weight=".3"
                    android:background="@drawable/img_tile_exercises"
                    android:gravity="center"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#bb000000"
                        android:orientation="vertical"
                        android:layout_alignParentBottom="true"
                        android:paddingLeft="6dp">

                        <TextView
                            android:id="@+id/textViewExerciseTitle"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="3dp"
                            android:text="@string/exercises"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:textColor="@color/colorWhite" />


                    </LinearLayout>
                </LinearLayout>
                <!-- //Row 1 - Column 2 -->

                <!-- Row 1 - Column 3 -->
                <LinearLayout
                    android:id="@+id/linearLayoutMyProfile"
                    android:layout_weight=".3"
                    android:layout_width="0dp"
                    android:layout_height="100dp"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:background="@drawable/img_tile_profile"
                    android:layout_marginTop="2dp"
                    android:layout_marginRight="1dp"
                    android:layout_marginLeft="2dp">


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:background="#bb000000"
                        android:orientation="vertical"
                        android:paddingLeft="6dp">

                        <TextView
                            android:id="@+id/textViewMyProfileTitle"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="3dp"
                            android:text="@string/my_profile"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:textColor="@color/colorWhite" />
                    </LinearLayout>
                </LinearLayout>
                <!-- //Row 1 - Column 3 -->
            </TableRow>

            <!-- //Row 1 -->
        </TableLayout>
        <!-- //Navigation -->


    </LinearLayout>
</ScrollView>

Only do this for each of your row will solve your problem.

<!-- Row 1 - Column 3 -->
                <LinearLayout
                    android:id="@+id/linearLayoutMyProfile"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="2dp"
                    android:layout_marginRight="1dp"
                    android:layout_marginTop="2dp"
                    android:layout_weight=".3"
                    android:background="@drawable/com_facebook_profile_picture_blank_square"
                    android:gravity="center"
                    android:orientation="vertical">


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_alignParentBottom="true"

                        android:gravity="bottom"
                        android:orientation="vertical"
                        android:paddingLeft="6dp">

                        <TextView
                            android:id="@+id/textViewMyProfileTitle"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="bottom"
                            android:background="#bb000000"
                            android:layout_marginTop="3dp"
                            android:text="5454"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:textColor="@color/white" />
                    </LinearLayout>
                </LinearLayout>

Hopefully it will solve your problem

android:layout_alignParentBottom is not a supported attribute of a LinearLayout . If you want to use that, you should convert the parent LinearLayout into a RelativeLayout that supports those relative attributes.

Or, a simpler option would be to just set the parent LinearLayout's gravity: android:gravity="bottom"

Also, why don't just set a background color inside a TextView?

android:background="@color/your_color"

Then you would be able to eliminate reduntant LinearLayouts wrapping TextViews

我想您想做的是将android:layout_gravity =“ bottom”放到您要放在底部的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