自定义视图中将忽略layout_weight属性

时间:2016-08-02 07:12:57

标签: android android-layout-weight

我尝试实现自定义视图并将其放在可扩展的卡片视图中,该视图包含在RecyclerView中。一开始,我打开它看起来很不错 滚动之前 Before scroll

然后,我将其打开并滚动到RecyclerView的底部。当我滚动回到项目,关闭它然后再次打开它时,布局被打破。
滚动后 After scroll

以下是自定义视图的代码

<?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="60dp" android:background="?android:attr/selectableItemBackground"
    android:clickable="true">
    <View
        android:layout_width="@dimen/session_padding_left"
        android:layout_height="@dimen/session_padding_left"></View>
    <TextView
        android:id = "@+id/tv_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textColor="@color/lightGray"
        android:gravity="center_vertical"
        android:layout_weight="6"
        android:maxLines = "2"
        android:lines="2"
        android:textSize="@dimen/extra_small_text_size"
        android:ellipsize="end"
        android:paddingTop = "10dp"
        android:paddingBottom = "10dp"
        android:text = "Word-of-Mouth Behaviour in Mobile Social Media  "/>
    <TextView
        android:id = "@+id/tv_time"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="right"
        android:textSize="@dimen/extra_small_text_size"
        android:textColor="@color/lightGray"
        android:layout_weight = "4"
        android:text="11:30 - 12:00"/>

    <ImageButton
        android:layout_width="@dimen/dimen_image_button_add_remove"
        android:layout_height="@dimen/dimen_image_button_add_remove"
        android:layout_gravity="center|right"
        android:scaleType="fitCenter"
        style = "?android:attr/borderlessButtonStyle"
        android:src = "@drawable/ic_add_agenda"
        />

</LinearLayout>

视图类:

package au.com.leremede.acis2016.views;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;
import au.com.leremede.acis2016.R;

public class PresentationView extends LinearLayout {
    private TextView tvTitle;
    private TextView tvTime;
    public PresentationView(Context context) {
        super(context);
        init(context);
    }

    public PresentationView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context);
    }


    private void init(Context context) {
        setOrientation(HORIZONTAL);
        inflate(context, R.layout.view_presentation, this);
        tvTitle = (TextView)findViewById(R.id.tv_title);
        tvTime = (TextView)findViewById(R.id.tv_time);
    }


    public void setTitle(String title)
    {
        tvTitle.setText(title);
    }

    public void setTime(String time)
    {
        tvTime.setText(time);
    }
}

1 个答案:

答案 0 :(得分:0)

我自己使用百分比支持库解决了这个问题

<?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="60dp" xmlns:app="http://schemas.android.com/apk/res-auto"
                android:background="?android:attr/selectableItemBackground"
                android:clickable="true">
    <View
            android:id="@+id/empty_view"
            android:layout_width="@dimen/session_padding_left"
            android:layout_height="@dimen/session_padding_left"
    ></View>
    <ImageButton
            android:id="@+id/btn_add_remove_agenda"
            android:layout_width="@dimen/dimen_image_button_add_remove"
            android:layout_height="@dimen/dimen_image_button_add_remove"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:scaleType="fitCenter"
            style="?android:attr/borderlessButtonStyle"
            android:src="@drawable/ic_add_agenda"
    />
    <android.support.percent.PercentRelativeLayout android:layout_width="wrap_content"
                                                   android:layout_height="match_parent"
                                                   android:layout_toRightOf="@id/empty_view"
                                                   android:layout_toLeftOf="@id/btn_add_remove_agenda">
        <TextView
                android:id="@+id/tv_title"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:textColor="@color/lightGray"
                android:layout_alignParentLeft="true"
                app:layout_widthPercent="60%"
                android:maxLines="2"
                android:lines="2"
                android:textSize="@dimen/extra_small_text_size"
                android:ellipsize="end"
                android:paddingTop="10dp"
                android:paddingBottom="10dp"
                android:text="Word-of-Mouth Behaviour in Mobile Social Media  "/>
        <TextView
                android:id="@+id/tv_time"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:gravity="right"
                android:layout_alignParentRight="true"
                app:layout_widthPercent="40%"
                android:textSize="@dimen/extra_small_text_size"
                android:textColor="@color/lightGray"
                android:text="11:30 - 12:00"/>
    </android.support.percent.PercentRelativeLayout>


</RelativeLayout>