13125

Layout items adjustment issue

Question:

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 <strong>dp to height & weight</strong>?

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.

<strong>XML</strong>

<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>

<strong>I want this</strong>

<a href="https://i.stack.imgur.com/Mnm5B.jpg" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/Mnm5B.jpg" data-original="https://i.stack.imgur.com/Mnm5B.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Answer1:

You have opened <RelativeLayout> but didn't closed it. Try this code:

<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>

Answer2:

If you will give constant height to card view it will not vary, and you can get your desired layout.

<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">

Recommend

  • How to position CardViews next to each other?
  • Transparent background on CardView - Android
  • LinearLayout flagged if used in CardView
  • How to apply shadow to ImageView?
  • Android custom dialog linearlayout size same as dialogs bg image
  • How to convert NAnt function “path::combine(path1, path2)” to MSBuild?
  • Savon soap body problem
  • Android : Strike out Text with bold or thicker line than default STRIKE_THRU_TEXT_FLAG
  • Background image crashes android
  • How to automatically pop-up keyboard?
  • Float Label hint (TextInputLayout) not works with Android Data Binding
  • Android Custom dialog inflating from layout - alignment issue
  • EditText ignores the ImeActionLabel
  • Translate animation to correct position in Android
  • Toolbar shifts up, when soft keyboard appears
  • Loop animation drawable
  • Gray line inside cardview :)?
  • Wrapping text in spinner android
  • Running jasmine tests for a component with NgZone dependency
  • How to resolve permission denied maybe missing internet permission?
  • Splash Screen will not display
  • WPF Template Binding in ToggleButton UserControl
  • Hibernate to update table schema
  • JBoss External Properties Files in Classpath
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • Code in Job's Script Block after Start-Process Does not Execute
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • Jackson Parser: ignore deserializing for type mismatch
  • Disable Enter in editText android
  • Change multiple background-images with jQuery
  • Cannot resolve symbol 'MyApi'
  • If I include Java 8 in my Android app does that affect which devices it will work on?
  • Pass value from viewmodel to script in zk
  • Trying to switch camera back to front but getting exception
  • Release, debug version and Authorization Google?
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy