【问题标题】:Relative Layout, Textview not showing相对布局,Textview 不显示
【发布时间】:2011-06-14 16:55:59
【问题描述】:

我正在使用此 XML 在底部显示我的一个 Button,在顶部显示一个 TextView,在中间显示一个 TextView。中间的文本视图覆盖了按钮和顶部文本视图之间的整个范围。中间的TextView 根本没有显示出来。怎么了?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/task"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="@string/tasks"
        />
    <Button 
        android:id="@+id/btnAddTask"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="@string/AddTasks"
        android:layout_alignParentBottom="true" 
        />
    <TextView 
        android:id="@+id/TasksList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="@string/tasks" 
        android:layout_above="@id/task"
        android:layout_below="@id/btnAddTask"
        />
</RelativeLayout>

【问题讨论】:

  • +1 用于在代码标签中显示整个代码而不会打扰版主。

标签: android android-layout textview


【解决方案1】:

您的问题肯定是您的按钮下方有 TasksList TextView。交换你的 layout_above 和 layout_below 值。这对我有用:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/task"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="@string/tasks"
        />
    <Button 
        android:id="@+id/btnAddTask"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="@string/AddTasks"
        android:layout_alignParentBottom="true" 
        />
    <TextView 
        android:id="@+id/TasksList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="@string/tasks" 
        android:layout_above="@id/btnAddTask"
        android:layout_below="@id/task"
        />
</RelativeLayout>

【讨论】:

    【解决方案2】:

    问题是您颠倒了上面的布局和下面的布局: 试试

    android:layout_below="@id/task"
    android:layout_above="@id/btnAddTask"
    

    看截图。

    这是完整的布局。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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/task"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="@string/tasks"
            />
        <Button 
            android:id="@+id/btnAddTask"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="@string/AddTasks"
            android:layout_alignParentBottom="true" 
            />
        <TextView 
            android:id="@+id/TasksList"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="@string/tasks" 
            android:layout_below="@id/task"
            android:layout_above="@id/btnAddTask"
            />
    </RelativeLayout>
    

    【讨论】:

    • 这不是 OP 所要求的……应该有 3 个视图……2 个 TextViews 和 1 个按钮。
    • 我认为灰色的顶部是 Activity 标题...不是此布局中的视图之一...
    • 好吧,你是对的。我没有引起足够的重视。我在上面发布了一个更正,以及一个新的屏幕抓取。
    【解决方案3】:

    你的布局肯定有问题。

    您有按钮,alignParentBottom="true"...然后对于 TasksList TextView,它被设置在按钮下方...基本上不在屏幕上。

    您可以从 RelativeLayout... 中删除方向...相对布局没有像 LinearLayout 那样的方向。

    正如你所描述的......我不是 100% 你可以用Relative Layout 做到这一点。如果您只是想要一个在顶部,一个居中,另一个在底部,最好为每个元素使用一个布局参数,centerInParentalignParentTopalignParentBottom

    如果你重新考虑LinearLayout(我猜你是从这里开始的)你会坚持垂直方向,然后给他们layout_weight 0、1 和0,确保高度设置为wrap_content。

    <?xml version="1.0" encoding="utf-8"?>
    <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/task"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:layout_weight="0" 
            />
        <TextView 
            android:id="@+id/TasksList"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="@string/tasks" 
            android:layout_weight="1" 
            />
        <Button 
            android:id="@+id/btnAddTask"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="@string/AddTasks"
            android:layout_weight="0"
            />
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2015-12-29
      • 1970-01-01
      • 2011-08-30
      • 1970-01-01
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多