线性布局权重适用于 android studio,不适用于设备
Linearlayout weight works on android studio, doesn't on device
在为 Listview 制作简单的项目布局时,我遇到了这个问题:
这是 android 工作室的样子(正确方式)
这是它在设备上的样子 (w)
好像重量在设备上不起作用!
这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
>
<ImageView
android:id="@+id/mp_imagen"
android:layout_width="130dp"
android:layout_height="130dp"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:background="#333333"
android:src="@drawable/menu_background"
/>
<TextView
android:id="@+id/mp_titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Título del producto"
android:singleLine="true"
android:background="#77000000"
android:padding="5dp"
android:textColor="#FFFFFF"
android:layout_alignRight="@+id/mp_imagen"
android:layout_alignLeft="@+id/mp_imagen"
android:ellipsize="end"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/mp_imagen"
android:layout_alignTop="@+id/mp_imagen"
android:layout_alignBottom="@+id/mp_imagen"
android:orientation="vertical"
android:weightSum="2"
android:layout_marginLeft="10dp"
>
<TextView
android:id="@+id/mp_interesados"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Interesados"
android:drawableLeft="@drawable/ic_interest"
android:gravity="center_vertical"
android:drawablePadding="10dp"
android:layout_weight="1"
/>
<TextView
android:id="@+id/mp_chats"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Chats"
android:drawableLeft="@drawable/ic_chat"
android:gravity="center_vertical"
android:drawablePadding="10dp"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>
使用 LinearLayout 作为根,在 RelativeLayout 中只将 Image 和 textView 放在 Image 上面。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#FFFFFF" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/mp_imagen"
android:layout_width="130dp"
android:layout_height="130dp"
..../>
<TextView
android:id="@+id/mp_titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
..../>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2"
android:layout_marginLeft="10dp"
>
<TextView
android:id="@+id/mp_interesados"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Interesados"
android:drawableLeft="@drawable/ic_interest"
android:gravity="center_vertical"
android:drawablePadding="10dp"
android:layout_weight="1"
/>
<TextView
android:id="@+id/mp_chats"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Chats"
android:drawableLeft="@drawable/ic_chat"
android:gravity="center_vertical"
android:drawablePadding="10dp"
android:layout_weight="1"
/>
</LinearLayout>
在为 Listview 制作简单的项目布局时,我遇到了这个问题:
这是 android 工作室的样子(正确方式)
这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
>
<ImageView
android:id="@+id/mp_imagen"
android:layout_width="130dp"
android:layout_height="130dp"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:background="#333333"
android:src="@drawable/menu_background"
/>
<TextView
android:id="@+id/mp_titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Título del producto"
android:singleLine="true"
android:background="#77000000"
android:padding="5dp"
android:textColor="#FFFFFF"
android:layout_alignRight="@+id/mp_imagen"
android:layout_alignLeft="@+id/mp_imagen"
android:ellipsize="end"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/mp_imagen"
android:layout_alignTop="@+id/mp_imagen"
android:layout_alignBottom="@+id/mp_imagen"
android:orientation="vertical"
android:weightSum="2"
android:layout_marginLeft="10dp"
>
<TextView
android:id="@+id/mp_interesados"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Interesados"
android:drawableLeft="@drawable/ic_interest"
android:gravity="center_vertical"
android:drawablePadding="10dp"
android:layout_weight="1"
/>
<TextView
android:id="@+id/mp_chats"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Chats"
android:drawableLeft="@drawable/ic_chat"
android:gravity="center_vertical"
android:drawablePadding="10dp"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>
使用 LinearLayout 作为根,在 RelativeLayout 中只将 Image 和 textView 放在 Image 上面。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#FFFFFF" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/mp_imagen"
android:layout_width="130dp"
android:layout_height="130dp"
..../>
<TextView
android:id="@+id/mp_titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
..../>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2"
android:layout_marginLeft="10dp"
>
<TextView
android:id="@+id/mp_interesados"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Interesados"
android:drawableLeft="@drawable/ic_interest"
android:gravity="center_vertical"
android:drawablePadding="10dp"
android:layout_weight="1"
/>
<TextView
android:id="@+id/mp_chats"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Chats"
android:drawableLeft="@drawable/ic_chat"
android:gravity="center_vertical"
android:drawablePadding="10dp"
android:layout_weight="1"
/>
</LinearLayout>