【发布时间】:2012-05-26 21:52:48
【问题描述】:
我有一个带有两个列表视图和两个死按钮的线性布局。如何在列表视图之间平均分配空间。我得到了一些建议,比如将高度设置为 0dip,但它没有用。 eclipse 中的图形布局显示了正确的布局,但是当我向列表中添加不同数量的元素时,它们会扩展到不同的高度。这是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="match_parent"
android:orientation="vertical"
android:weightSum="2">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:text="Installed engines" >
</Button>
<ListView
android:id="@+id/primary"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:text="Active engines" >
</Button>
<ListView
android:id="@+id/secondary"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
</LinearLayout>
谢谢
由于没有人相信我,这里是我所得到的图像。 只是链接,因为我没有足够的声誉来发布照片:( ! run ! eclipse
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
毕竟这可能不是基于这个答案Linear Layout and weight in Android的xml问题
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
这对于静态 XML 布局是正确的。如果您在运行时动态添加视图,则需要使用带有布局参数的 addView,例如 addView(button, new LinearLayout.LayoutParams(0, height, 1));即使您使用正确的宽度和重量值来膨胀布局也是如此。 – Nuthatch 2011 年 9 月 3 日 21:41
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
我确实使用布局充气器将我的视图添加到选项卡。也许这就是问题所在.. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
这是我使用充气机时重现问题的示例代码。
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ParticipantsPanel p = new ParticipantsPanel(this);
setContentView(p);
}
}
class ParticipantsPanel extends LinearLayout {
public ParticipantsPanel(Context ctx) {
super(ctx);
LayoutInflater li = LayoutInflater.from(ctx);
View myView = li.inflate(R.layout.participants, null);
final ListView primary = (ListView) myView.findViewById(R.id.primary);
final ListView secondary = (ListView) myView.findViewById(R.id.secondary);
final ArrayAdapter<String> primaryA =
new ArrayAdapter<String>(ctx,R.layout.lvtext);
final ArrayAdapter<String> secondaryA =
new ArrayAdapter<String>(ctx,R.layout.lvtext);
primaryA.add("hello1");
primaryA.add("hello2");
primaryA.add("hello3");
primaryA.add("hello4");
primaryA.add("hello5");
primaryA.add("hello6");
primaryA.add("hello7");
primaryA.add("hello8");
secondaryA.add("select1");
secondaryA.add("select2");
primary.setAdapter(primaryA);
secondary.setAdapter(secondaryA);
addView(myView);
}
}
xxxxxxxxxxxxxxxxxxxxxxxxxx
又一次更新。 我认为问题出在 Nuthatch(我在一篇旧文章中提到的那个人)指出:addView() 只会弄乱静态 xml 中的任何布局,这是 imo 的宝贵经验。当我将 ParticipantsPanel 中的代码移动到主要活动并直接设置视图而不将其添加到另一个线性布局时,它按预期工作。我很感激解释为什么会这样,以及我如何仍然可以将我的代码放在一个类中并具有所需的行为。
【问题讨论】:
-
那是因为你应该设置
layout_height="0dp" -
我在帖子中提到过,但它没有用。请注意,它按预期工作,两者中的元素数量相同。但并非如此。
-
是的,我读过。这就是为什么这是评论而不是答案,尽管是答案。
-
你说这是答案吗?如果您可以修改 xml 以使其按预期工作,我将不胜感激。
-
将两个
ListViews的layout_height设置为0dp,因为K-ballo 已经说过将使列表具有相同的高度(即使两个ListViews的行数不同) )。这到底怎么不适合你?