android - 如何在线性布局中居中 TextView

标签 android view android-linearlayout

我有以下布局,我想让 textview 出现在 View 的中心和中间。我怎样才能做到这一点?

在图形 View 中查看布局时,我尝试调整各种重力属性,但似乎没有任何改变。我想要中间的 TextView ,我已经完成了,但在 View 的一半。

感谢马特。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/carefreebgscaledalphajpg" >



    <TextView
        android:id="@+id/textviewdatefornocallspage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="You have no calls for "
        android:textSize="25sp" />

</LinearLayout>

最佳答案

在你的linearLayout标签中将重力设置为中心:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     android:gravity="center"
    android:background="@drawable/carefreebgscaledalphajpg" >

或像这样使用 RelativeLayout :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/carefreebgscaledalphajpg" >



    <TextView
        android:id="@+id/textviewdatefornocallspage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="You have no calls for "
        android:textSize="25sp" />

</RelativeLayout>

关于android - 如何在线性布局中居中 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13474982/

相关文章:

java - 返回 SWT/JFace View

Android支持库23.2.0设置样式报错="@style/Widget.AppCompat.Button.Colored"

java - GC 会在 Android 上自动处理/释放在 jni 中分配的内存吗?

ruby-on-rails - Rails - ActiveAdmin - 自定义表单将 has_many 关系显示为表

Grails:如何从 Controller 逻辑返回错误消息(即不是源自域属性验证)?

android linearlayout on relativelayout 使 relative 消失

安卓布局师

android - 在添加另一个 View 之前检查是否存在来自 layoutinflater 的 View

android - 您需要在此 Activity 中使用 Theme.AppCompat 主题(或后代)

java - 以编程方式限制 EditText 以显示数字键盘