0

считывает данные с сервера и показывает это в виде карты. я делаю так много способов, чтобы придать весу карте, но не работает!layout_weight не работает для CardView

это мой код:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 



    <android.support.v7.widget.CardView 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     card_view:cardCornerRadius="5dp"> 


      <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="10dp" 
      android:orientation="vertical"> 


      <ImageView 
       android:layout_gravity="center" 
       android:id="@+id/img" 
       android:scaleType="fitXY" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 

      <TextView 
       android:id="@+id/tv_name" 
       android:layout_marginTop="10dp" 
       android:layout_marginBottom="10dp" 
       android:textSize="18sp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textStyle="bold" /> 
      <TextView 
       android:id="@+id/tv_version" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
      <TextView 
       android:id="@+id/tv_api_level" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 

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

и выглядеть следующим образом:

enter image description here

, но хочу, чтобы мой вид карты выглядеть следующим образом:

enter image description here

как можно я делаю это ? Важным является: не хочу указывать высоту номер как 50dp хочу дать вес причина во всех устройствах выглядят как pic2. пожалуйста, помогите Тпх

+0

веса работать с LinearLayout, не cardview – piotrek1543

+0

знаю, что! но я всегда помещаю свои элементы в макет лайнера, а затем придаю ему вес. если у вас есть какое-то решение, дайте мне не научить меня – erfan

ответ

2

вам нужно использовать android:weightSum должным образом, давая android:layout_height="0dp", если у вас есть android:orientation="vertical"

проверка ниже XML код,

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

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

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" 
      android:layout_marginTop="5dp" 
      android:layout_weight="1" 
      android:elevation="10dp" 
      card_view:cardCornerRadius="5dp"> 

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

       <ImageView 
        android:id="@+id/img1" 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_gravity="center" 
        android:layout_weight="7" 
        android:scaleType="fitXY" 
        android:src="@mipmap/ic_true"/> 

       <TextView 
        android:id="@+id/tv_name1" 
        android:layout_width="wrap_content" 
        android:layout_height="0dp" 
        android:layout_marginBottom="5dp" 
        android:layout_marginTop="5dp" 
        android:layout_weight="1" 
        android:gravity="center" 
        android:text="test" 
        android:textSize="18sp" 
        android:textStyle="bold"/> 

       <TextView 
        android:id="@+id/tv_version1" 
        android:layout_width="wrap_content" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:gravity="center" 
        android:text="test"/> 

       <TextView 
        android:id="@+id/tv_api_level1" 
        android:layout_width="wrap_content" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:gravity="center" 
        android:text="test"/> 
      </LinearLayout> 
     </android.support.v7.widget.CardView> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"/> 

    </LinearLayout> 
</LinearLayout> 
+0

спасибо за ответ, но проблема в том, что я читаю изображение и текст с сервера, поэтому в моих java-кодах я помещаю тему один за другим и не могу поместить данные в 2-х изображение или текстовое представление! – erfan

+0

Я не совсем понимаю ваш вопрос, теперь вы можете добавить этот макет программно, и ваша проблема будет решена. –

+0

да и правильно ур ответ правильный. но может сделать этот размер макета лайнера для 1 вид карты? u сделайте два макета и вид карты! Я хочу этот размер, но не два! возможно? – erfan

Смежные вопросы