Android-线性布局权重在Listview项中无法正常工作 [英] Android - Linear layout weight not working properly in listview items

查看:102
本文介绍了Android-线性布局权重在Listview项中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android Studio中看似正确,但在手机上执行时不起作用.
我使用的是一个简单的线性布局(水平),其中包含两个线性布局(垂直)容器,每个容器应占用列表视图中行的50%的空间.这是代码:

Looks right in Android Studio, but doesn't work when executed on my phone.
I'm using a simple linear layout (horizontal) with two linear layout (vertical) containers inside, which should each take up 50% of the space of the row in the listview. Here's the code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="0.5">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="001"
                android:id="@+id/pokemonNumber"
                android:layout_marginLeft="5dp" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Bulbasaur"
                android:id="@+id/pokemonName"
                android:layout_marginLeft="10dp" />

        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Grass"
                android:id="@+id/pokemonTypes"
                android:layout_marginLeft="50dp" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="0.5">

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/pokemonImage"
            android:src="#ff65ff4f" />

    </LinearLayout>

</LinearLayout>

如果您需要视觉参考,我可以上传图片,但是基本上它应该是两个线性布局,每个线性布局分别占其父级线性布局的50%.有任何想法为什么这行不通吗?

I can upload pictures if you need a visual reference, but basically it should just be two linear layouts taking up 50% each of their parent linear layout, horizontally. Any ideas why this isn't working?

为清晰起见,附上图片:

attached pictures for clarity:

在Android Studio中的外观(正确): http://i.stack.imgur. com/bxhxi.png

How it looks in Android Studio (correct): http://i.stack.imgur.com/bxhxi.png

它的实际外观(不正确): http://i.stack.imgur.com/jPXCW.png

How it actually looks (incorrect): http://i.stack.imgur.com/jPXCW.png

这是列表视图的行布局

this is a row layout for a listview

使用此布局的代码:

public class DexListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<Pokemon> mPokes;
    private String mRowLayout = "pokemon_list_item";
    private Context mContext;

    public DexListAdapter(Context context, List<Pokemon> pokes) {
        mInflater = LayoutInflater.from(context);
        mPokes = pokes;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mPokes.size();
    }

    @Override
    public Object getItem(int position) {
        return mPokes.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;

        if(convertView == null) {
            int resID = mContext.getResources().getIdentifier(mRowLayout, "layout", mContext.getPackageName());
            view = mInflater.inflate(resID, parent, false);

            holder = new ViewHolder();

            holder.number = (TextView)view.findViewById(R.id.pokemonNumber);
            holder.name = (TextView)view.findViewById(R.id.pokemonName);
            holder.types = (TextView)view.findViewById(R.id.pokemonTypes);
            holder.image = (ImageView)view.findViewById(R.id.pokemonImage);

            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder)view.getTag();
        }

        Pokemon poke = mPokes.get(position);
        //Set info from pokemon object to listview item
        holder.number.setText(poke.getStringNumber());
        holder.name.setText(poke.getName());
        holder.types.setText(poke.getTypeOne() + " " + poke.getTypeTwo());

        return view;
    }

    private class ViewHolder {
        public TextView number, name, types;
        public ImageView image;
    }
}

在此处调用的代码上方(作为导航抽屉活动中使用的片段的一部分):

Above code called here (as part of fragment used in navigation drawer activity):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_pokelist, container, false);
    ListView l1=(ListView)rootView.findViewById(R.id.pokeList);
    l1.setAdapter(new DexListAdapter(c,pokemonList));
    return rootView;
}

列表视图的代码

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

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pokeList" />
</LinearLayout>

推荐答案

问题是ListView在wrap_content上具有宽度,这不允许LinearLayout使用其权重属性.因此解决方案如下所示:

The problem was that the ListView had it's width on wrap_content which didn't allow the LinearLayout to use it's weight properties. So the solution is as shown below:

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

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pokeList" />
</LinearLayout>

我在这里的一个问题中也读过它(不幸的是找不到链接),给ListView wrap_content作为高度不是很好,总是要像match_parent一样具体或使用其他约束,以便它知道之前的高度任何孩子都被添加.

I also read it in a question here (Can't find the link unfortunately) that it's not good to give a ListView wrap_content as height, always try to be specific, like match_parent or use other constraints so it knows it's height before any children are added.

这篇关于Android-线性布局权重在Listview项中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆