在Android Studio中看起来很正确,但在我的手机上执行时却无法正常工作 我使用了一个简单的线性布局(水平),里面有两个线性布局(垂直)容器,每个容器应占据listview中行的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:这是listview的行布局
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;
}
}
上面的代码在这里调用(作为导航抽屉活动中使用的片段的一部分):
@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:listview的代码
<?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>
答案 0 :(得分:4)
问题是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>
我也在这里的一个问题中读到它(遗憾的是找不到链接)将ListView wrap_content作为高度给出是不好的,总是尝试特定的,比如match_parent或使用其他约束,所以它知道它的高度之前任何孩子都加入了。
答案 1 :(得分:0)
尝试更换:
view = mInflater.inflate(resID, parent, false);
与
view = mInflater.inflate(resID, null, false);
第一个说&#34;用我的父母找出我的布局参数&#34;,第二个说&#34;使用我的布局资源的布局参数设置&#34;。