【问题标题】:Views not visible in Relativelayout在相对布局中不可见的视图
【发布时间】:2017-10-30 16:59:08
【问题描述】:

This is the layout image 我正在尝试在相对布局的图像上添加文本,三个垂直线。 带有 ID 的视图,Leftline 和 rightline 不可见,而带有 ID cardline 的视图可见。如何使所有视图可见?

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="6dp">
        <!--app:cardBackgroundColor="#000000"-->


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp">




            <ImageView
                android:layout_marginBottom="40dp"
                android:id="@+id/main_image_story"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:contentDescription="@string/contentdescimage"
                android:src="@drawable/vijaymallya"
                android:scaleType="fitXY"
                android:adjustViewBounds="true"/>

            <View
                android:id="@+id/leftline"
                android:layout_width="4dp"
                android:layout_height="match_parent"
                android:background="#f49a32"/>


            <View
                android:id="@+id/rightline"
                android:layout_width="4dp"
                android:layout_height="match_parent"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:background="#f49a32"/>




            <View
                android:id="@+id/cardline"
                android:layout_width="match_parent"
                android:layout_height="6dp"
                android:background="#f49a32" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/cardline"
                android:background="@drawable/background_gradient"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/date_story"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="3dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginStart="10dp"
                    android:layout_marginTop="8dp"
                    android:shadowColor="#000000"
                    android:shadowRadius="2"
                    android:textColor="#ffffff"
                    android:textSize="10sp"
                    tools:text="9.30 PM, 28 APRIL 2017" />



            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="@drawable/background_gradient2"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="15dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="15dp"
                    android:layout_marginStart="10dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent">

                        <View
                            android:layout_width="5dp"
                            android:layout_height="wrap_content"
                            android:layout_marginEnd="11dp"
                            android:layout_marginRight="11dp"
                            android:background="#ffffff"/>

                    </LinearLayout>


                    <TextView
                        android:id="@+id/substory_title_story"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:lineSpacingExtra="0sp"
                        android:shadowColor="#000000"
                        android:shadowRadius="2"
                        android:textColor="#ffffff"
                        android:textSize="16sp"
                        tools:text="Vijay Mallya Misled Us On Wealth, Says Supreme Court, Hauls Him For Contempt" />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tags"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="9dp"
                    android:layout_marginRight="9dp"
                    android:layout_marginTop="10dp"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="6" />

                    <TextView
                        android:id="@+id/number_of_articles_story"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="4"
                        android:gravity="end"
                        android:shadowColor="#000000"
                        android:shadowRadius="2"
                        android:textColor="#f49a32"
                        android:textSize="11sp"
                        tools:text="7 ARTICLES - EXPLORE" />

                </LinearLayout>
            </LinearLayout>

        </RelativeLayout>


     </android.support.v7.widget.CardView>

    </LinearLayout>

【问题讨论】:

  • 你的CardViewlayout_height="wrap_content"RelativeLayoutlayout_height="match_parent" .... 这显然没有意义... 为什么?您告诉父视图与直接子视图一样长,还告诉子视图与父视图一样长......这意味着无法计算高度
  • 将相对布局更改为换行内容,视图仍然不可见
  • @Jani 可以在这里上传布局图片吗?
  • @LokeshDesai 添加了布局图片
  • @Jani 请检查我的回答..

标签: android xml android-relativelayout


【解决方案1】:

请尝试此更改..我已在我的演示应用中尝试过它正在运行

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_marginBottom="6dp">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">


                <ImageView
                    android:id="@+id/main_image_story"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:layout_marginBottom="40dp"
                    android:adjustViewBounds="true"
                    android:scaleType="fitXY"
                    android:src="@drawable/dubai" />

                <View
                    android:id="@+id/leftline"
                    android:layout_width="4dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="start"
                    android:background="#f49a32" />

                <View
                    android:id="@+id/rightline"
                    android:layout_width="4dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="end"
                    android:background="#f49a32" />
            </FrameLayout>


            <View
                android:id="@+id/cardline"
                android:layout_width="match_parent"
                android:layout_height="6dp"
                android:background="#f49a32" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/cardline"
                android:background="@color/white"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/date_story"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="3dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginStart="10dp"
                    android:layout_marginTop="8dp"
                    android:shadowColor="#000000"
                    android:shadowRadius="2"
                    android:textColor="#ffffff"
                    android:textSize="10sp"
                    tools:text="9.30 PM, 28 APRIL 2017" />


            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="@color/white"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="15dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="15dp"
                    android:layout_marginStart="10dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent">

                        <View
                            android:layout_width="5dp"
                            android:layout_height="wrap_content"
                            android:layout_marginEnd="11dp"
                            android:layout_marginRight="11dp"
                            android:background="#ffffff" />

                    </LinearLayout>


                    <TextView
                        android:id="@+id/substory_title_story"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:lineSpacingExtra="0sp"
                        android:shadowColor="#000000"
                        android:shadowRadius="2"
                        android:textColor="#ffffff"
                        android:textSize="16sp"
                        tools:text="Vijay Mallya Misled Us On Wealth, Says Supreme Court, Hauls Him For Contempt" />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tags"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="9dp"
                    android:layout_marginRight="9dp"
                    android:layout_marginTop="10dp"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="6" />

                    <TextView
                        android:id="@+id/number_of_articles_story"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="4"
                        android:gravity="end"
                        android:shadowColor="#000000"
                        android:shadowRadius="2"
                        android:textColor="#f49a32"
                        android:textSize="11sp"
                        tools:text="7 ARTICLES - EXPLORE" />

                </LinearLayout>
            </LinearLayout>

        </RelativeLayout>


    </android.support.v7.widget.CardView>

</LinearLayout>

希望这对你有帮助...如果不是请告诉我...会找到其他方法...

【讨论】:

  • 谢谢,它有效。但是你能告诉我我之前的代码有什么问题吗?
  • @Jani 首先你有图像视图,属性为 match_parent....scale type=fitxy 和 adjustviewbound=true...这就是为什么你的 relativelayout 允许图像覆盖左右视图...没有别的......否则你的代码很好
  • 在你提供的 framelayout 中,我设置了 scale type=fitxy 和 adjustviewbound=true 以使图像不缩小(否则图像正在缩小),在这种情况下可以看到视图。我必须让 adjustviewbound=true,否则我的图像正在缩小。为什么它只发生在相对布局中?
  • 没有没有你的代码很好......每个布局都有自己的行为框架布局将明智地显示布局层,所以对于你的要求,使用它是完美的......
【解决方案2】:

根据您的布局使用填充和边距

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:layout_margin="5dp"
    android:orientation="vertical">
  .....
</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多