【问题标题】:Align TextView centerly and to the left with each other将 TextView 居中和向左对齐
【发布时间】:2019-05-07 04:04:17
【问题描述】:

我在垂直 LinearLayout 中有 3 个 TextView,如下所示:

<?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/green"
    tools:context=".SplashActivity">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:gravity="center"
            android:text="FirstText"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:gravity="center"
            android:text="Second"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/lightGray"
            android:gravity="center"
            android:text="ThirdText"/>

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

以上代码的当前输出:

但我希望文本像这样彼此左对齐:

我如何做到这一点?

【问题讨论】:

    标签: android alignment


    【解决方案1】:

    您需要有一个将其内容居中的顶级父级。在其中,您将拥有一个左对齐 内容的中级父级。在其中你将拥有三个 TextView。

    完成这项工作的关键是让中级父级的宽度与其内容一样宽。这样,最宽的孩子将定义中级父级的整体大小,然后顶级父级可以居中它们的组。

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="FirstText"/>
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Second"/>
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ThirdText"/>
    
        </LinearLayout>
    
    </FrameLayout>
    

    【讨论】:

      【解决方案2】:
       <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical"        
          android:layout_gravity="center">
      
          <TextView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:textColor="@color/colorPrimary"            
              android:text="FirstText"/>
      
          <TextView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:textColor="@color/colorPrimary"      
              android:text="Second"/>
      
          <TextView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:textColor="@color/colorPrimary"        
              android:text="ThirdText"/>
      
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 2018-05-21
        • 1970-01-01
        • 2014-08-10
        • 1970-01-01
        • 2013-05-22
        • 2014-04-24
        • 2017-12-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多