【问题标题】:Multiple Text Views don't align properly in Linear Layout多个文本视图在线性布局中未正确对齐
【发布时间】:2012-07-05 10:10:21
【问题描述】:

我正在尝试在我相当简单的布局中添加 8 个文本视图。 我想每行有两个文本视图和四个这样的行。 我的挑战是在此布局中排列第 2、第 4、第 6 和第 8 个文本视图。 第 1、3、5、7 个文本视图的文本长度不同。

我无法将每行中的 seocnd 文本视图排列成垂直对齐。

基本上,这些文本视图需要从垂直对齐的同一行开始,无论该行中第一个文本视图的长度是多少:

有人可以帮忙吗?

这是我的 XML。

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/productDetailsLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="vertical" >

<include
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/logo_logout" />


<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp" >

    <TextView
        android:id="@+id/ProductDetailsTitle"
        android:layout_width="wrap_content"
        android:layout_height="43dp"
        android:layout_marginTop="10dp"
        android:background="@color/Navy"
        android:gravity="center"
        android:text="@string/ProductDetailsTitle"
        android:textColor="@color/white"
        android:textSize="20dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/ProductDetailsEditButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_weight="0.36"
        android:text="Edit"
        android:textSize="20dp" />
</LinearLayout>

<include
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    layout="@layout/product_details_mainpage_part1" />

<include
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    layout="@layout/product_details_mainpage_part2" />

<include
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    layout="@layout/product_details_mainpage_part3" />

<include
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    layout="@layout/product_details_mainpage_part44" />

</LinearLayout>

【问题讨论】:

    标签: android layout textview alignment


    【解决方案1】:

    也许这会有所帮助。您可以使用每个TextViewbackground colortop padding 来使其成为您想要的样子。希望这可以帮助。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:baselineAligned="false"
        android:id="@+id/RelativeLayout"
        android:background="#ffffffff"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <LinearLayout
            android:id="@+id/background"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >
    
        <TextView
            android:id="@+id/ProductDetailsTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffffff"
            android:gravity="center"
            android:paddingTop="50dp"
            android:text="Hello, I am tired" />
    
        <TextView
            android:id="@+id/ProductDetailsTitle2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffffff"
            android:gravity="center"
            android:paddingTop="50dp"
            android:text="Hello" />
    
        <TextView
            android:id="@+id/ProductDetailsTitle3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffffff"
            android:gravity="center"
            android:paddingTop="50dp"
            android:text="Hello" />
    
        <TextView
            android:id="@+id/ProductDetailsTitle4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffffff"
            android:gravity="center"
            android:paddingTop="50dp"
            android:text="Hello" />
       </LinearLayout>
    
    
       <LinearLayout
           android:id="@+id/background2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:orientation="vertical" >
    
        <TextView
            android:id="@+id/ProductDetailsTitle5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"            
            android:background="#ffffffff"
            android:paddingTop="50dp"
            android:gravity="center"
            android:text="Hello" />
    
        <TextView
            android:id="@+id/ProductDetailsTitle6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffffff"
            android:gravity="center"
            android:paddingTop="50dp"
            android:text="Hello" />
    
        <TextView
            android:id="@+id/ProductDetailsTitle7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffffff"
            android:gravity="center"
            android:paddingTop="50dp"
            android:text="Hello" />
    
        <TextView
            android:id="@+id/ProductDetailsTitle8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffffff"
            android:gravity="center"
            android:paddingTop="50dp"
            android:text="Hello, something" />
    
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      我知道这是一个旧帖子,但我也一直在努力解决这个问题。 我发现最有效的是膨胀视图,然后设置新的线性布局参数。这将导致两行和单行文本视图正确垂直居中对齐。

          LayoutInflater inflater = (LayoutInflater) this.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          TextView textView = (TextView) inflater.inflate(R.layout.workout_summary_textview, null);
          inflater = null;
      
          LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
          params.gravity = Gravity.CENTER;
      
          textView.setLayoutParams(params);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-01
        • 2015-04-18
        • 2022-01-19
        • 2019-06-03
        • 1970-01-01
        • 2012-09-27
        • 2017-01-20
        • 2016-05-13
        相关资源
        最近更新 更多