Android 如何将线性布局与活动底部对齐
我有以下activity_main.xml文件,我想将LinearLayout中的三个按钮与页面底部对齐。我尝试过布局\u alignParentBotton=“true”Android 如何将线性布局与活动底部对齐,android,layout,android-constraintlayout,Android,Layout,Android Constraintlayout,我有以下activity_main.xml文件,我想将LinearLayout中的三个按钮与页面底部对齐。我尝试过布局\u alignParentBotton=“true” 但这不起作用。。是否有任何设置可以让我将线性布局带到活动的底部? 谢谢 将父约束布局替换为RelativeLayout将父约束布局替换为RelativeLayout将以下约束添加到线性布局中 app:layout_constraintBottom_toBottomOf="parent" app:layout_const
但这不起作用。。是否有任何设置可以让我将线性布局带到活动的底部? 谢谢
将父约束布局替换为RelativeLayout将父约束布局替换为RelativeLayout将以下约束添加到线性布局中
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>
将以下约束添加到线性布局中
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>
将android:layout\u alignParentBottom=“true”更改为
应用程序:layout\u constraintBottom\u 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">
...
。。。
...
将android:layout\u alignParentBottom=“true”更改为应用程序:layout\u constraintBottom\u 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">
...
。。。
...
尝试以下方式编辑布局:
<?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>
请注意线性布局中的布局权重。尝试以下方式编辑布局:
<?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
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">
您的根布局是一个ConstraintLayout
,因此您应该使用中列出的适当约束来定位其子级。要将线性布局放置在底部中心并包裹其内容,应使用以下命令:
<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">
试试这个
<?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>
试试这个
<?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>
不要对整个线性布局应用android:layout\u alignParentBottom=“true”
,而是尝试对线性布局中的单个元素应用它。这不起作用,而不是对整个线性布局应用android:layout\u alignParentBottom=“true”
,尝试将其应用于LinearLayout中的单个元素。这不起作用。您必须重新排列其他视图,但LinearLayout与ImageView将在底部对齐。您必须重新排列其他视图,但LinearLayout与ImageView将在底部对齐。我不熟悉如何回答中的问题堆栈溢出,所以不要介意我的代码格式化。我不熟悉在堆栈溢出中回答问题,所以不要介意我的代码格式。对于ConstraintLayout中包含的小部件,不建议使用MATCH_PARENT。类似的行为可以通过使用MATCH_约束来定义,相应的左/右或上/下约束设置为“parent”。对于ConstraintLayout中包含的小部件,不建议使用MATCH_parent。类似的行为可以通过使用MATCH_约束来定义,并将相应的左/右或上/下约束设置为“parent”。