繁体   English   中英

如何将LinearLayout与活动底部对齐

[英]How to Align LinearLayout to Bottom of Activity

我有以下activity_main.xml文件,并且我想将LinearLayout中的三个按钮与页面底部对齐。 我已经尝试过layout_alignParentBotton =“ true”,但是那不起作用..是否有任何设置可以让我将linearLayout置于活动的底部? 谢谢

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blueBackground"
    tools:context="org.pctechtips.george.dailyquotes.MainActivity">

        <TextView
            android:id="@+id/main_title"
            android:layout_width="368dp"
            android:layout_height="wrap_content"
            android:text="Daily Quotes"
            android:textSize="30dp"
            android:textColor="@color/whiteText"
            android:textAlignment="center"
            tools:layout_editor_absoluteX="8dp"
            tools:layout_editor_absoluteY="16dp" />

        <ImageView
            android:id="@+id/main_image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            app:srcCompat="@mipmap/ic_launcher_round"
            android:layout_centerHorizontal="true"
            tools:layout_editor_absoluteY="89dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent" />


        <TextView
            android:id="@+id/quote_text"
            android:layout_width="368dp"
            android:layout_height="wrap_content"
            android:text="This is a random text"
            android:textColor="@color/whiteText"
            android:textSize="20dp"
            tools:layout_editor_absoluteX="8dp"
            tools:layout_editor_absoluteY="246dp" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true">

            <ImageView
                android:id="@+id/previous_quote"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@android:drawable/ic_media_previous"
                tools:layout_editor_absoluteX="8dp"
                tools:layout_editor_absoluteY="473dp" />

            <ImageView
                android:id="@+id/share_quote"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:layout_editor_absoluteX="8dp"
                tools:layout_editor_absoluteY="473dp" />

            <ImageView
                android:id="@+id/next_quote"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@android:drawable/ic_media_next"
                tools:layout_editor_absoluteX="8dp"
                tools:layout_editor_absoluteY="473dp" />

        </LinearLayout>
</android.support.constraint.ConstraintLayout>

android:layout_alignParentBottom="true"更改为app:layout_constraintBottom_toBottomOf="parent" 这是一个示例,该示例还将布局扩展到父级的宽度并将视图居中

...
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent">
...

在线性布局中添加以下约束。

 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintStart_toStartOf="parent"

您的某些观点未正确约束。 如果要使用RelativeLayout尝试以下解决方案。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blueBackground"
    tools:context="org.pctechtips.george.dailyquotes.MainActivity">

    <TextView
        android:id="@+id/main_title"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:text="Daily Quotes"
        android:textSize="30dp"
        android:textAlignment="center"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="16dp" />

    <ImageView
        android:id="@+id/main_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@+id/main_title"
        app:srcCompat="@mipmap/ic_launcher_round"
        android:layout_centerHorizontal="true"
        />


    <TextView
        android:id="@+id/quote_text"
        android:layout_width="match_parent"
        android:layout_below="@+id/main_image"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:text="This is a random text"
        android:textSize="20dp"

        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true">

        <ImageView
            android:id="@+id/previous_quote"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@android:drawable/ic_media_previous"
            />

        <ImageView
            android:id="@+id/share_quote"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

        <ImageView
            android:id="@+id/next_quote"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@android:drawable/ic_media_next"
            />

    </LinearLayout>
</RelativeLayout>

将父约束布局替换为RelativeLayout

尝试通过以下方式编辑布局:

<?xml version="1.0" encoding="utf-8"?>     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:background="@color/blueBackground" tools:context="org.pctechtips.george.dailyquotes.MainActivity"> 
<LinearLayout android: layout_weight="1" android orientation="vertical" android:layout_width="match_parent" android:layout_height="0dp">
<TextView android:id="@+id/main_title" android:layout_width="368dp" android:layout_height="wrap_content" android:text="Daily Quotes" android:textSize="30dp" android:textColor="@color/whiteText" android:textAlignment="center" /> <ImageView android:id="@+id/main_image" android:layout_width="100dp" android:layout_height="100dp" app:srcCompat="@mipmap/ic_launcher_round" /> <TextView android:id="@+id/quote_text" android:layout_width="368dp" android:layout_height="wrap_content" android:text="This is a random text" android:textColor="@color/whiteText" android:textSize="20dp"/>
</LinearLayout>
 <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true"> <ImageView android:id="@+id/previous_quote" android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@android:drawable/ic_media_previous"/> <ImageView android:id="@+id/share_quote" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:id="@+id/next_quote" android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@android:drawable/ic_media_next"/> </LinearLayout> </LinearLayout>

注意线性布局中的布局权重。

根布局是ConstraintLayout因此应使用文档中列出的适当约束来定位其子级。 为了将LinearLayout放置在底部中心并包装其内容,您应该使用以下命令:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constrainedWidth="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

您还可以通过完全删除LinearLayout并将三个ImageViews ConstraintLayout为根ConstraintLayout直接子代来优化布局,如下所示:

<ImageView
    android:id="@+id/previous_quote"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@android:drawable/ic_media_previous"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@id/share_quote"
    app:layout_constraintStart_toStartOf="parent" />

<ImageView
    android:id="@+id/share_quote"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@id/next_quote"
    app:layout_constraintStart_toEndOf="@id/previous_quote"/>

<ImageView
    android:id="@+id/next_quote"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@android:drawable/ic_media_next"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@id/share_quote" />

尝试这个..

<?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf- 
8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light">

<TextView
    android:id="@+id/main_title"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Daily Quotes"
    android:textAlignment="viewStart"
    android:textColor="@android:color/white"
    android:textSize="30dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<ImageView
    android:id="@+id/main_image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerHorizontal="true"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:srcCompat="@mipmap/ic_launcher_round"
    tools:layout_editor_absoluteY="89dp" />


<TextView
    android:id="@+id/quote_text"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:text="This is a random text"
    android:textColor="@android:color/white"
    android:textSize="20dp"
    app:layout_constraintTop_toBottomOf="@id/main_image" />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <ImageView
        android:id="@+id/previous_quote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@android:drawable/ic_media_previous" />

    <ImageView
        android:id="@+id/share_quote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/next_quote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@android:drawable/ic_media_next" />

</LinearLayout>
</android.support.constraint.ConstraintLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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