Layout items adjustment issue

时间:2018-09-19 08:20:14

标签: android android-layout

I am trying to design layout like below but I am unable to design. I don't know how set layout without dp. How can I do without assigning dp to height & weight?

Items should be centered with same size of all card views and is it possible to not assign dp to image view height width but all images should be of same size.

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">

<android.support.v7.widget.CardView
    android:id="@+id/card_view_news"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_marginBottom="10dp"
    android:background="@drawable/bg"
    card_view:cardCornerRadius="5dp"
    card_view:cardElevation="2dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp"
        android:weightSum="2">

        <ImageView
            android:id="@+id/NewsImage"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scaleType="fitXY" />

        <TextView
            android:id="@+id/NewsTitle"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".166"
            android:ellipsize="end"
            android:hint="News"
            android:singleLine="true"
            android:textColor="@color/primarytext"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/NewsDescription"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".5"
            android:ellipsize="end"
            android:hint="No News Feed"
            android:maxLines="3"
            android:text="TextView"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/News_Channel"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight=".166"
            android:background="@drawable/bg"
            android:ellipsize="end"
            android:maxLines="1"
            android:padding="5dp"
            android:text="TextView"
            android:textColor="#FFFF"
            android:textSize="10sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/News_Time"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight=".166"
            android:text="TextView"
            android:textColor="@color/colorPrimary"
            android:textSize="10sp"
            android:textStyle="bold" />

    </LinearLayout>

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

I want this

enter image description here

2 个答案:

答案 0 :(得分:0)

您已打开<RelativeLayout>,但没有关闭它。尝试以下代码:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view_news"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginBottom="10dp"
        android:background="@drawable/bg"
        card_view:cardCornerRadius="5dp"
        card_view:cardElevation="2dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="10dp"
            android:weightSum="2">

            <ImageView
                android:id="@+id/NewsImage"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/NewsTitle"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight=".166"
                android:ellipsize="end"
                android:hint="News"
                android:singleLine="true"
                android:textColor="@color/primarytext"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/NewsDescription"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight=".5"
                android:ellipsize="end"
                android:hint="No News Feed"
                android:maxLines="3"
                android:text="TextView"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/News_Channel"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight=".166"
                android:background="@drawable/bg"
                android:ellipsize="end"
                android:maxLines="1"
                android:padding="5dp"
                android:text="TextView"
                android:textColor="#FFFF"
                android:textSize="10sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/News_Time"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight=".166"
                android:text="TextView"
                android:textColor="@color/colorPrimary"
                android:textSize="10sp"
                android:textStyle="bold" />

        </LinearLayout>

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

</RelativeLayout>

答案 1 :(得分:0)

如果您将名片视图的高度保持不变,则高度不会变化,并且您可以获得所需的布局。

 <android.support.v7.widget.CardView
    android:id="@+id/card_view_news"
    android:layout_width="wrap_content"
    android:layout_height="300dp"
    android:layout_centerInParent="true"
    android:layout_marginBottom="10dp"
    android:background="@drawable/bg"
    card_view:cardCornerRadius="5dp"
    card_view:cardElevation="2dp">