如何使用自动换行自动更改布局高度?

时间:2017-06-15 14:03:41

标签: android xml layout height adapter

如您所见,高度设置通常只有一个文本字符串,但所有崩溃,当它被包裹时:

enter image description here

这是我的XML:

<?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:background="?android:attr/selectableItemBackground"
    android:focusable="true"
    android:orientation="horizontal"
    android:paddingBottom="10dp"
    android:paddingStart="15dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:paddingEnd="15dp"
>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
    >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/title"
            android:textSize="16sp"
            android:textStyle="bold"
            android:layout_marginTop="10dp"
        />

        <TextView
            android:id="@+id/descr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
        />

    </LinearLayout>

    <CheckBox
        android:id="@+id/checkbox"
        android:theme="@style/checkBoxStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:clickable="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_marginTop="7dp"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
    />

</LinearLayout>

这是我的适配器:

class AppsAdapter extends RecyclerView.Adapter<AppsAdapter.ViewHolder> {

    private Activity activity;
    private ArrayList<Item> items;
    private int inflater;

    private OnItemCheckListener onItemClick;

    AppsAdapter (Activity activity, ArrayList<Item> items, int inflater, OnItemCheckListener onItemCheckListener) {

        this.activity = activity;
        this.items = items;
        this.inflater = inflater;
        this.onItemClick = onItemCheckListener;

    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView icon;
        private TextView title, descr, version;
        private CheckBox checkbox;

        private ViewHolder (View view) {

            super (view);

            icon = (ImageView) view.findViewById (R.id.icon);

            title = (TextView) view.findViewById (R.id.title);
            descr = (TextView) view.findViewById (R.id.descr);
            version = (TextView) view.findViewById (R.id.version);

            checkbox = (CheckBox) view.findViewById (R.id.checkbox);

        }

    }

    @Override
    public ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
        return new ViewHolder (LayoutInflater.from (parent.getContext ()).inflate (inflater, parent, false));
    }

    @Override
    public void onBindViewHolder (final ViewHolder holder, int position) {

        Item item = items.get (position);

        if (holder.icon != null)
        holder.icon.setImageDrawable (item.icon);

        holder.title.setText (item.getItem (0));
        holder.descr.setText (item.getItem (1));

        // Other actions...

    }

}

那么,真的要这样做吗?我不想使用标准的Android设置活动,因为我想支持KitKat(4.4),但其默认主题很糟糕(

或者它可能是TableLayout的解决方案,依此类推?

提前致谢!

2 个答案:

答案 0 :(得分:1)

你使用你的父布局高度使用像这样的android:layout_height =“wrap_content”

用户你的身高android:layout_width =“match_parent”

绝对是你的o / p

答案 1 :(得分:0)

  

更改xml

<?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:background="?android:attr/selectableItemBackground"
    android:focusable="true"
    android:gravity="center"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingBottom="5dp"
        android:paddingTop="5dp">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/colorPrimary"
            android:textSize="16sp"
            android:text="abababababababababbabababababababab"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/descr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="abababababababababbababababababababsdhsjdhsadsahdkjsahdsadsadhsadhsauidhsauidhsauihsauihsaduisahduisahduisahduisahdusaidhsauidhsauidhsauiidhsauidhsauidhsauidhsauidhsaudhsauidhsadiusahduisahdusahu"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </LinearLayout>

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:layout_marginTop="7dp"
        android:clickable="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:theme="@style/checkBoxStyle" />

</LinearLayout>