【问题标题】:TextView not displaying above RadioGroupTextView 未显示在 RadioGroup 上方
【发布时间】:2018-04-06 12:58:29
【问题描述】:

在我的布局中,我有一个带有两个 RadioButtons 的 RadioGroup。我在 RadioGroup 上放了一个 TextView。

但 TextView 没有显示在 RadioGroup 上方。只有水平的单选按钮是可见的。布局代码如下:-

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbarstart"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"                
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


</android.support.design.widget.AppBarLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioGroup
            android:layout_marginTop="60dp"
            android:id="@+id/radiogroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/radioButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/east"
                android:textSize="18sp"/>

            <RadioButton
                android:id="@+id/radioButton2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/west"
                android:textSize="18sp"/>

        </RadioGroup>

     <TextView
     android:id="@+id/textView1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_above="@+id/radiogroup"
     android:layout_centerHorizontal="true"
     android:layout_marginBottom="25dp"
     android:text="Vehicle Direction"
     />

    </RelativeLayout>

 </android.support.design.widget.CoordinatorLayout>

请指导解决问题。

【问题讨论】:

  • RelativeLayout是和appbarlayout重叠的问题,使用线性布局或者框架布局会显示textview

标签: android android-layout


【解决方案1】:

试试这个

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbarstart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.AppBarLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioGroup
            android:id="@+id/radiogroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="60dp"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/radioButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="east"
                android:textSize="18sp" />

            <RadioButton
                android:id="@+id/radioButton2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="west"
                android:textSize="18sp" />
        </RadioGroup>
    </RelativeLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/radiogroup"
        android:layout_marginBottom="10dp"
        android:text="Vehicle Direction"
        app:layout_anchor="@id/radiogroup" />
</android.support.design.widget.CoordinatorLayout>

输出

【讨论】:

  • 谢谢尼莱什。这是工作。但是你能解释一下为什么我的布局代码没有按预期工作吗?
  • @John 我用过pp:layout_anchor="@id/radiogroup"
猜你喜欢
  • 2014-08-11
  • 1970-01-01
  • 1970-01-01
  • 2018-12-25
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 2018-05-19
  • 1970-01-01
相关资源
最近更新 更多