【问题标题】:Using RelativeLayout使用相对布局
【发布时间】:2011-12-21 15:14:39
【问题描述】:

我想做这样的布局:

问题在于对齐 TextView:我希望它们都水平居中。我唯一的解决方案是在根RelativeLayout 中使用嵌套的LinearLayout。 我如何将 TextViews 水平居中只使用根 RelativeLayout,而不使用嵌套的 LinearLayout

【问题讨论】:

  • 你能发布你的xml吗?如果您不太习惯使用此处提到的布局,我可以建议您将水平线性布局嵌套在垂直线性布局中。

标签: android android-layout


【解决方案1】:

假设 id 0f imageview 是图像。然后对于第一个textView,您可以执行以下操作:

android:layout_alignLeft="@id/image"

对于第二个textView

android:layout_alignRight="@id/image"

您还可以在这样做之后在textViews 中留出一些余量

【讨论】:

    【解决方案2】:

    请试试这个::

    更新:

     <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:id="@+id/asdww"
        android:background="#000000">
        <TextView android:layout_width="wrap_content" android:id="@+id/textView1"
            android:layout_height="wrap_content" android:text="TextView"
            android:layout_centerHorizontal="true" android:layout_marginTop="60dp" />
        <TextView android:layout_width="wrap_content" android:id="@+id/textView2"
            android:layout_height="wrap_content" android:text="new aaaaaa"
            android:layout_alignParentLeft="true" android:layout_centerHorizontal="true"
            android:layout_marginTop="60dp" android:layout_below="@+id/textView1"
            android:layout_marginLeft="50dp"
    
             />
    
    
        <TextView android:layout_width="wrap_content" android:id="@+id/textView3"
            android:layout_height="wrap_content" android:text="new aaaaaa"
            android:layout_alignParentRight="true"
            android:layout_centerHorizontal="true" android:layout_marginTop="60dp"
            android:layout_below="@+id/textView1" 
            android:layout_marginRight="50dp"
    
            />
    
    </RelativeLayout>
    

    【讨论】:

    • 视图仍将在右侧和左侧居中,但不在中心。
    • 感谢您的回答,但我想知道是否可以在没有边距的情况下制作它。
    • 相对布局没有更多参数,所以我们需要继续这个
    【解决方案3】:

    在你的LinearLayout

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
        <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/> 
    
        <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>    
    
    </LinearLayout>
    

    据我所知,文本视图应以自己为中心。你可以实现android:layout_marginLeft

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多