Android - 线性布局权重在列表视图项目中无法正常工作

Android - Linear layout weight not working properly in listview items

在 Android Studio 中看起来不错,但在我的 phone.
上执行时不起作用 我使用的是一个简单的线性布局(水平),内部有两个线性布局(垂直)容器,每个容器应该占据列表视图中行的 space 的 50%。这是代码:

<?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%。知道为什么这不起作用吗?

编辑:为清晰起见,附上图片:

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

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

EDIT2:这是列表视图的行布局

EDIT3:使用此布局的代码:

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;
    }
}

以上代码在此处调用(作为导航抽屉中使用的片段的一部分 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;
}

EDIT4:列表视图的代码

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

尝试替换:

view = mInflater.inflate(resID, parent, false);

view = mInflater.inflate(resID, null, false);

第一个说"use my parent to figure out my layout params",第二个说"use my layout resource's layout params settings"。

问题是 ListView 的宽度 wrap_content 不允许 LinearLayout 使用它的权重属性。所以解决方法如下图:

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

我在这里的一个问题中也看到了(不幸的是找不到link),给ListView wrap_content作为高度是不好的,总是尽量具体,比如match_parent 或使用其他约束,以便在添加任何子项之前知道它的高度。