【问题标题】:Align 2 textviews horizontally android水平对齐2个文本视图android
【发布时间】:2017-09-22 18:14:55
【问题描述】:

我的应用程序屏幕 (https://i.stack.imgur.com/xFozp.png)

如图所示,我已水平对齐文本视图。当产品名称较长时,名称在文本视图中显示为 2 行。 但是当 textview 中的行数增加时,两个 textview 的起始行不会水平对齐。 我希望文本视图的起始行水平对齐。 目前我正在使用线性布局。我应该改变布局吗?谁能给出要写的代码。 因此,即使 2 个文本视图彼此相邻(如图所示),它们的起始(第一行)也应该在同一水平线上。

我的代码如下-

<?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:layout_margin="10dp"
    android:background="@drawable/b22"
    android:gravity="center|left"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="15dp"
        android:maxLines="3"  />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:maxLines="3"  />

</LinearLayout>

【问题讨论】:

  • 发布您的布局....代码
  • 嗨,添加了我的布局代码。

标签: android layout android-linearlayout text-alignment


【解决方案1】:

1.LinearLayout中删除属性android:gravity="center|left"

2.textView2 中删除属性android:layout_gravity="right|center" 并将其宽度更改为android:layout_width="match_parent"

试试这个:

<?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="wrap_content"
    android:layout_margin="10dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:text="Product Name -"
        android:textColor="@android:color/black"
        android:textSize="15dp"
        android:maxLines="3"  />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is Liberty Agile Pension Preserver Product"
        android:textColor="@android:color/black"
        android:maxLines="3"  />

</LinearLayout>

输出:

对于您的设计,最好使用单个 RelativeLayout 并将您的所有 TextViews 放入其中。

这是一个例子:

<?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="wrap_content"
    android:layout_margin="10dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:text="Product Name -"
        android:textColor="@android:color/black"
        android:textSize="15dp"
        android:maxLines="3"  />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/textView1"
        android:layout_alignTop="@id/textView1"
        android:text="This is Liberty Agile Pension Preserver Product"
        android:textColor="@android:color/black"
        android:maxLines="3"  />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView2"
        android:layout_marginTop="20dp"
        android:layout_marginRight="10dp"
        android:text="Number of Senarios Executed -"
        android:textColor="@android:color/black"
        android:textSize="15dp"
        android:maxLines="3"  />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/textView3"
        android:layout_alignTop="@id/textView3"
        android:text="400"
        android:textColor="@android:color/black"
        android:maxLines="3"  />

</RelativeLayout>

输出:

希望对你有帮助~

【讨论】:

    【解决方案2】:

    考虑将LinearLayout 用于两个TextViewsweightSum 的组。从总和中给他们两个权重。下面是一个例子

    <LinearLayout
      android:id="@+id/text_layout"
      android:orientation="horizontal"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginRight="15dp"
      android:layout_marginLeft="15dp"
      android:layout_marginTop="5dp"
      android:weightSum="2"
      android:background="@android:color/white">
    
      <TextView
        android:id="@+id/tvProductNameLabel"
        android:layout_width="0dp"
        android:layout_weight="0.6"
        android:layout_height="wrap_content"
        android:text="Product Name"
        android:gravity="right"
        android:textSize="14sp"
        android:layout_marginRight="10dp"/>
    
      <TextView
        android:id="@+id/tvProductName"
        android:layout_width="0dp"
        android:layout_weight="1.4"
        android:textSize="14sp"
        android:textColor="@android:color/black"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_marginLeft="10dp"
        android:layout_gravity="left"/>
     </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2017-08-30
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2013-04-25
      • 2015-12-17
      • 2012-03-02
      • 1970-01-01
      • 2016-08-01
      相关资源
      最近更新 更多