【问题标题】:How to align text one below the other in android?如何在android中将文本对齐到另一个下方?
【发布时间】:2014-06-13 20:33:57
【问题描述】:

我正在开发安卓应用。在这里,我有 2 个TextView,我需要将其中一个对齐到另一个下方。(i.e "Artist Name" should be exact below the "Title of song" )

任何人都可以帮助我将TextView 对齐到另一个下方。 提前致谢。

下面是我的代码

<RelativeLayout xmlns:
  android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="@drawable/list_selector"
  android:orientation="horizontal"
  android:padding="5dip" >

  <TableLayout
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:layout_centerHorizontal="true" >

  <!--  Left side Thumbnail image -->
  <TableRow android:id="@+id/thumbnail"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="3dip"
   android:layout_alignParentLeft="true"
   android:background="@drawable/image_bg"
   android:layout_marginRight="5dip">

<ImageView
    android:id="@+id/list_image"
    android:layout_width="50dip"
    android:layout_height="50dip"
    android:src="@drawable/doc"/>



 <!-- Title Of Song-->
 <TextView
 android:id="@+id/title"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignTop="@+id/thumbnail"
 android:layout_toRightOf="@+id/thumbnail"
 android:text="love you the"
 android:textColor="#040404"
 android:typeface="sans"
 android:textSize="15dip"
 android:textStyle="bold"/>

 <!-- Artist Name -->
  <TextView
  android:id="@+id/artist"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_below="@id/title"
  android:textColor="#343434"
  android:textSize="10sp"
  android:layout_marginTop="1dip"
  android:layout_toRightOf="@+id/thumbnail"
  android:text="Mary" />

  <!-- Rightend Duration -->
  <TextView
 android:id="@+id/duration"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentRight="true"
 android:layout_alignTop="@id/title"
 android:gravity="right"
 android:text="7:45"
 android:layout_marginRight="5dip"
 android:textSize="10dip"
 android:textColor="#10bcc9"
 android:textStyle="bold"/>

    <!-- Rightend Arrow -->
     <ImageView android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:src="@drawable/arrows"
 android:layout_alignParentRight="true"
 android:layout_centerVertical="true"/>
 </TableRow>
 </TableLayout>

   </RelativeLayout>

【问题讨论】:

  • 这里的诀窍是 android:gravity 是正确的属性,使用它。
  • 这是您的程序检查链接的最佳示例。 stackoverflow.com/questions/14000755/…
  • 尝试使用LinearLayout或者使用属性android:layout_alignBottom="@+id/title"
  • @Jay,没用 :(
  • @Ramki Anba,已经使用,android:layout_below="@+id/title"

标签: android alignment textview


【解决方案1】:

只需使用this 将您的物品与其他物品相关联。

androidHacker 是对的,你可以像这样将一个文本视图放在另一个的下方:

android:layout_below="@+id/your_another_text_view"

类似的布局是本教程中的here,您正在此处查看。

【讨论】:

  • 文本仍然相同(即未在“歌曲标题下方对齐”)
  • 只需从 haresh answer@user3607124 中删除这一行 android:layout_centerHorizo​​ntal="true" >
  • @ Robi Kumar Tomar ,删除 android:layout_centerHorizo​​ntal="true" > 整个内容都向左对齐
  • 您是否阅读了我在此处附加的最后一个链接,它显示的视图与您想要的相同。就用这个pastebin.com/4xy2KEWu@user3607124
  • @Robi Kumar Tomar,一定会完成并让你知道谢谢
【解决方案2】:
// try this way,hope this will help you to solve your problem.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" >

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true" >

        <!--  Left side Thumbnail image -->
        <TableRow android:id="@+id/thumbnail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:background="@drawable/image_bg"
            android:layout_marginRight="5dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ImageView
                    android:id="@+id/list_image"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_alignParentLeft="true"
                    android:src="@drawable/ic_launcher"/>


                <TextView
                    android:id="@+id/title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@id/list_image"
                    android:text="love you the"
                    android:textColor="#040404"
                    android:typeface="sans"
                    android:textSize="15sp"
                    android:textStyle="bold"/>

                <!-- Artist Name -->
                <TextView
                    android:id="@+id/artist"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/title"
                    android:layout_toRightOf="@id/list_image"
                    android:textColor="#343434"
                    android:textSize="12sp"
                    android:layout_marginTop="1dp"
                    android:text="Mary" />

                <TextView
                    android:id="@+id/duration"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@id/title"
                    android:gravity="right"
                    android:text="7:45"
                    android:layout_marginRight="5dp"
                    android:textSize="12sp"
                    android:textColor="#10bcc9"
                    android:textStyle="bold"/>

                <ImageView
                    android:id="@+id/arrow_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher"
                    android:layout_alignParentRight="true"
                    android:layout_toRightOf="@id/duration"
                    android:layout_centerVertical="true"/>

            </RelativeLayout>
        </TableRow>
    </TableLayout>
</RelativeLayout>

【讨论】:

  • @Haresh,谢谢 :) 它帮助了我
  • @Haresh,文本对齐,但整个内容在中心对齐:(
  • 尝试从 TableLayout 中删除此属性 android:layout_centerHorizo​​ntal="true"。
【解决方案3】:

像这样尝试..

 android:layout_below="@+id/your_another_text_view"

更新

您只需要专门为该 textView 使用相对布局.. 尝试并告诉我。

【讨论】:

    【解决方案4】:
     <TextView
     android:id="@+id/title"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignTop="@+id/thumbnail"
     android:layout_toRightOf="@+id/thumbnail"
     android:text="love you the"
     android:textColor="#040404"
     android:typeface="sans"
     android:textSize="15dip"
     android:textStyle="bold"/>
    
     <!-- Artist Name -->
      <TextView
      android:id="@+id/artist"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_below="@id/title"
      android:textColor="#343434"
     android:layout_alignLeft="@+id/title"// This line is new
      android:textSize="10sp"
      android:layout_marginTop="1dip"
    
      android:text="Mary" />
    

    【讨论】:

    • @Wasim Ahmed,没有帮助:(
    • 你已经接受了一个答案..所以我认为你不需要任何答案:)
    【解决方案5】:

    尝试使用LinearLayout或者使用属性android:layout_alignBottom="@+id/title"

    【讨论】:

      猜你喜欢
      • 2016-09-25
      • 2021-06-28
      • 1970-01-01
      • 2019-01-08
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      相关资源
      最近更新 更多