【发布时间】:2014-07-22 15:16:59
【问题描述】:
我正在开发一个显示页面的应用程序,该页面具有可变数量的方形按钮,例如“Square MIUI”的第二张图片:
我只需要显示三列方形按钮,并使整个列表可滚动。
我首先尝试使用纯 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1.19"
android:orientation="horizontal" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Title"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<!-- Icon buttons here -->
</TableRow>
</TableLayout>
</ScrollView>
<Button
android:layout_width="fill_parent"
android:layout_height="48dp"
android:text="Button" />
</LinearLayout>
对于每个按钮(3 个按钮的 4 个 TableRow,图像是使用 Eclipse 图标生成器制作的方形 .png) 编辑:选中,所有屏幕密度的图标都是方形的
<Button
android:layout_margin="4dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="image"/>
但我平板电脑上的按钮是方形的,而我手机上的按钮比高宽(在 X 轴上拉伸)。
我做错了什么?
TODO:对可变按钮编号进行编码。
提前致谢。
编辑:
我试过这段代码:
private void squareButton() {
square(R.id.b1);
square(R.id.b2);
....
square(R.id.b<N>);
}
private void square(int id) {
ImageButton temp=(ImageButton) findViewById(id);
int l=temp.getWidth();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int Measuredwidth = metrics.widthPixels;
l=(int) (Measuredwidth/4);
temp.setMaxWidth(l);
temp.setMaxHeight(l);
temp.setMinimumWidth(l);
temp.setMinimumHeight(l);
LayoutParams param = temp.getLayoutParams();
param.width = l;
param.height = l;
temp.setLayoutParams(param);
temp.requestLayout();
}
但我的手机按钮仍然很奇怪(姜饼)... 我不能只依赖自动调整大小的图标(平板电脑太小,手机太大)
编辑: 我正在寻找这样的代码:
- 方块按钮
- 具有用户可定义的宽度
- 与姜饼一起使用
在我的例子中,button_width = widthPixels/4;
提前谢谢你。
【问题讨论】: