注意:我原本想在Pastebin上发布所有这些XML,但我认为将其存档在此处对于后人来说很重要,因此请原谅代码示例的长度!拥有更多信息总比没有好,对吧?
考虑以下类似计算器的布局。(我仅为了本帖硬编码了字符串 - 它们通常是资源。)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/calculator"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:orientation="vertical"
android:stretchColumns="*">
<TableRow android:id="@+id/tableRowFieldName"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textViewFieldName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:layout_marginTop="0dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="5dp"
android:padding="0dp"
android:text="Label"
android:textSize="20dp"
android:textColor="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow android:id="@+id/tableRowDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textViewDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:layout_margin="5dp"
android:layout_marginTop="0dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="5dp"
android:padding="0dp"
android:text="Value"
android:textSize="35dp"
android:textColor="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.2">
<Button android:id="@+id/buttonA"
style="@style/ParameterButton"
android:text="Param A" />
<Button android:id="@+id/buttonB"
style="@style/ParameterButton"
android:text="Param B" />
</TableRow>
<TableRow android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.2">
<Button android:id="@+id/buttonC"
style="@style/ParameterButton"
android:text="Param C" />
<Button android:id="@+id/buttonD"
style="@style/ParameterButton"
android:text="Param D" />
</TableRow>
<TableRow android:id="@+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.2">
<Button android:id="@+id/buttonE"
style="@style/ParameterButton"
android:text="Param E" />
<Button android:id="@+id/buttonF"
style="@style/ParameterButton"
android:text="Param F" />
</TableRow>
<TableRow android:id="@+id/tableRow4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.4">
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"
android:layout_weight="1.0" />
<TableLayout android:id="@+id/calcKeypad"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:stretchColumns="*">
<TableRow
android:layout_weight="0.25"
android:gravity="center">
<Button android:id="@+id/button7"
style="@style/KeypadButton"
android:text="7" />
<Button android:id="@+id/button8"
style="@style/KeypadButton"
android:text="8" />
<Button android:id="@+id/button9"
style="@style/KeypadButton"
android:text="9" />
</TableRow>
<TableRow
android:layout_weight="0.25"
android:gravity="center">
<Button android:id="@+id/button4"
style="@style/KeypadButton"
android:text="4" />
<Button android:id="@+id/button5"
style="@style/KeypadButton"
android:text="5" />
<Button android:id="@+id/button6"
style="@style/KeypadButton"
android:text="6" />
</TableRow>
<TableRow
android:layout_weight="0.25"
android:gravity="center">
<Button android:id="@+id/button1"
style="@style/KeypadButton"
android:text="1" />
<Button android:id="@+id/button2"
style="@style/KeypadButton"
android:text="2" />
<Button android:id="@+id/button3"
style="@style/KeypadButton"
android:text="3" />
</TableRow>
<TableRow
android:layout_weight="0.25"
android:gravity="center">
<Button android:id="@+id/buttonClear"
style="@style/KeypadButton"
android:text="C" />
<Button android:id="@+id/button0"
style="@style/KeypadButton"
android:text="0" />
<Button android:id="@+id/buttonDecimal"
style="@style/KeypadButton"
android:text="." />
</TableRow>
</TableLayout>
<LinearLayout android:id="@+id/linearLayoutTotal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button android:id="@+id/buttonTotalWeight"
style="@style/FunctionButton"
android:text="Function A" />
<Button android:id="@+id/buttonTotalCost"
style="@style/FunctionButton"
android:text="Function B" />
<Button android:id="@+id/buttonAbout"
style="@style/FunctionButton"
android:layout_height="0dp"
android:layout_weight="0.25"
android:text="Function C" />
</LinearLayout>
</TableRow>
</TableLayout>
这里使用了以下样式(基本上是从原始XML中分离出来的):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CalcButton">
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">15dp</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:layout_margin">2dp</item>
</style>
<style name="ParameterButton" parent="CalcButton">
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">0.5</item>
</style>
<style name="KeypadButton" parent="CalcButton">
<item name="android:textSize">20dp</item>
</style>
<style name="FunctionButton" parent="CalcButton">
<item name="android:layout_height">0dp</item>
<item name="android:layout_weight">0.375</item>
</style>
</resources>
如果您将所有这些内容放入Android项目中,则应该在顶部看到三行两个参数按钮,下面是数字键盘,数字键盘右侧有三个功能按钮。大多数情况下都是无害的。(此外,它只会在纵向使用,因此不必担心考虑横向。)
然而...请特别注意XML中的ListView。您目前还看不到它,因为它具有android:visibility="gone"。想法是,在某些时候,我会将其删除,然后将数字键盘(相邻的TableLayout)设置为android:visibility="gone",从而显示列表。
换句话说,期望的效果是使列表和数字键盘占用完全相同的空间,以便我可以随意在两者之间切换。
我的问题是,一旦填充了ListView,它似乎打算占用整个表格的高度,摧毁可怜的参数按钮并将三个功能按钮带上路。
您可以通过设置一个非常基本的ArrayAdapter(我使用了android.R.layout.simple_list_item_1和android.R.id.text1)并将一个简单的String数组传递给它来看到这一点。目前,手动将“gone”赋值从列表移动到相邻的表格布局中(或在代码中执行此操作)。
设置固定高度似乎不可行,因为这应该在各种显示屏上工作。实际上,我甚至尝试了一个也许是错误的努力:
ListView list = (ListView) findViewById(android.R.id.list);
TableLayout calcKeypad = (TableLayout) findViewById(R.id.calcKeypad);
list.layout(calcKeypad.getLeft(), calcKeypad.getTop(), calcKeypad.getRight(), calcKeypad.getBottom());
calcKeypad.setVisibility(View.GONE);
list.setVisibility(View.VISIBLE);
不行,那也没帮助。
按照文档的要求,我甚至给ListView赋予了正确的ID,因为我想自定义它的布局。当然,我没有使用ListActivity,所以在这种情况下可能并不重要。(我只是使用普通的Activity。是的,我也尝试将其更改为ListActivity。但是都不行。)
不过,我完全可以接受这是我的错误。(事实上,我就指望着这个。) 我已经到了头撞墙的地步了。“一定有更好的方法!”
再次强调,我正在努力保持布局的流动性,以便在各种显示器尺寸(不是平板电脑,只是手持设备)上能够很好地工作,因此选择了这种布局。如果有更好的方法可以实现相同的效果,我非常乐意倾听。
底线:如何使ListView仅占用相邻计算器键盘(TableLayout)占用的区域?
欢迎提供提示/答案/指导!