简体   繁体   English

线性布局将元素向右对齐

[英]Linear layout align element to right

I have a linear layout that i use for a list view with a custom adapter, it consists in an image and two text views. 我有一个线性布局,用于带有自定义适配器的列表视图,它包含一个图像和两个文本视图。 My problem is that the first text view length is not always the same so i dont know how to align every second text view to the same position. 我的问题是,第一个文本视图的长度并不总是相同,所以我不知道如何将每个第二个文本视图对齐到同一位置。

row.xml row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_gravity="center"
    android:layout_width="48dp"
    android:layout_margin="5dp"
    android:layout_height="48dp"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textView1"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:text="TextView" />
<TextView
    android:id="@+id/textView2"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/txtresultadocolor"
    android:textSize="25dp"
    android:layout_marginLeft="15dp"
     />

This is my code now but the second text view always changes. 现在这是我的代码,但是第二个文本视图始终会更改。 I would appreciate any help. 我将不胜感激任何帮助。

You can give android:layout_weight="0.3" to the first TextView , which will make the second TextView be attached to one position and the size of the first TextView will increase according to the length of the text. 您可以将android:layout_weight="0.3"赋予第一个TextView ,这将使第二个TextView附加到一个位置,并且第一个TextView的大小将根据文本的长度而增加。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="0.3"
    android:text="TextViewd1391381231283123100"
    android:textSize="25dp" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="15dp"
    android:text="adfafasdfs"
    android:textColor="@color/Red"
    android:textSize="25dp" />

I guess the issue here is android:layout_gravity="center". 我猜这里的问题是android:layout_gravity =“ center”。 The textviews are getting displayed center aligned and the display length will vary as per their text sizes and they will not be aligned to the same position. 文本视图将显示为居中对齐,并且显示长度将根据其文本大小而变化,并且它们将不会对齐到同一位置。

Once you remove it and add android:layout_marginLeft="15dp" to the first textview also both textviews should become aligned to the same position in the left. 删除它并将android:layout_marginLeft =“ 15dp”添加到第一个textview后,两个textview也应对齐到左侧的相同位置。

要将视图右对齐,请使用android:layout_gravity="right"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM