android - 如何在线性布局中保持 TextView 为中心
问题描述
这是我的 xml 代码,我希望将中心保持Textview
在包含textview
和查看的线性布局内,但我希望文本视图在线性布局底部显示中心以查看,即按钮textview
应该从顶部视图和底部居中linearLayout
。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/no_vouchers_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="@+id/xxlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/header_height"
android:visibility="gone"
tools:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/margin_12"
android:orientation="vertical">
<TextView
android:id="@+id/xxmessage"
style="@style/Typeface.Body.TextMidGrey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineSpacingExtra="@dimen/tfl_6"
tools:text="KJGSAHJDGJKSGDASAGDHJGAS" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="@dimen/margin_16"
android:background="@color/text_light_grey" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/button"
style="@style/Typeface.Body.Bold.White"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_16"
android:paddingBottom="@dimen/padding_12"
android:paddingTop="@dimen/padding_12"
android:background="@drawable/welcome_screen_button_selector"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/convert_points_to_vouchers"
android:textAllCaps="false" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
解决方案
使用android:layout_width="match_parent"
&
android:gravity="center"
你的 TextView
它会工作
用这个
<TextView
android:id="@+id/xxmessage"
style="@style/Typeface.Body.TextMidGrey"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:lineSpacingExtra="@dimen/tfl_6"
tools:text="KJGSAHJDGJKSGDASAGDHJGAS" />
而不是这个
<TextView
android:id="@+id/xxmessage"
style="@style/Typeface.Body.TextMidGrey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineSpacingExtra="@dimen/tfl_6"
tools:text="KJGSAHJDGJKSGDASAGDHJGAS" />
示例代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/no_vouchers_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="@+id/xxlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/header_height"
android:visibility="gone"
tools:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/margin_12"
android:orientation="vertical">
<TextView
android:id="@+id/xxmessage"
style="@style/Typeface.Body.TextMidGrey"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:lineSpacingExtra="@dimen/tfl_6"
tools:text="KJGSAHJDGJKSGDASAGDHJGAS" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="@dimen/margin_16"
android:background="@color/text_light_grey" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/button"
style="@style/Typeface.Body.Bold.White"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_16"
android:paddingBottom="@dimen/padding_12"
android:paddingTop="@dimen/padding_12"
android:background="@drawable/welcome_screen_button_selector"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/convert_points_to_vouchers"
android:textAllCaps="false" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
推荐阅读
- javascript - 使用 php 进行 Ajax 调用
- ibm-mobilefirst - 证书固定在本机 android Mobilefirst 8.0 应用程序中不起作用
- android - Listview OnItemClickListener 在 MainActivity 上工作,但不在类似的 TaskActivity 上
- android - Firestore 无法获取文档,因为客户端处于脱机状态
- ajax - codeignitter 中的 ajax 表单 - 发送生成的表单
- mysql - 添加连接后 MySQL 查询中断
- tableau-api - 在日期之间计数的计算字段
- php - 使用 PHP 将 JSON 转换为 HTML 表或 div
- android - 两个 ArrayList 比较和更新一个 Arraylist 还是在 android 中覆盖?
- javascript - 如何检查对象是否包含 JavaScript 数组中的文本?