【发布时间】:2012-03-10 21:13:47
【问题描述】:
n.b.:我打算在 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);
不,这也没有帮助。
根据the documentation,我什至给了ListView 正确的ID,因为我想自定义它的布局。当然,我没有使用 ListActivity,所以在这种情况下可能没关系。 (我只是使用常规的 Activity。是的,我也尝试将其更改为 ListActivity。没有骰子。)
不过,我完全可以接受这是飞行员的错误。 (事实上,我指望它。)我正处于近视模式变成撞墙模式的地步。 “一定有更好的方法!”
再次请记住,我正在努力保持布局流畅,以便它适用于各种显示器尺寸(不是平板电脑,只是手持设备),因此选择了布局。如果有更好的方法来实现相同的视觉效果,我会全力以赴。
底线:如何使 ListView 只占用相邻计算器键盘(TableLayout)占用的区域?
感谢线索/答案/指导!
【问题讨论】:
标签: android android-layout android-listview