如何解决如何创建具有相等宽度的按钮?
| 我想在屏幕中间显示三个按钮,并且三个按钮的宽度相同,尽管它们的文本标签长度不同。 只需添加三个带有不同长度文本标签的按钮,就会产生不同宽度的按钮。<?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:layout_gravity=\"center_vertical\"
android:gravity=\"center\">
<Button
android:id=\"@+id/button_1\"
android:layout_height=\"fill_parent\"
android:layout_width=\"wrap_content\"
android:text=\"ABCDEF\" />
<Button
android:id=\"@+id/button_2\"
android:layout_height=\"fill_parent\"
android:layout_width=\"wrap_content\"
android:text=\"GHI\" />
<Button
android:id=\"@+id/button_3\"
android:layout_height=\"fill_parent\"
android:layout_width=\"wrap_content\"
android:text=\"JKLM\" />
</LinearLayout>
默认的按钮宽度包装内容:
-
将所有按钮上的layout_weight设置为1并将layout_width设置为0dip会使它们均匀拉伸以填充整个屏幕宽度。对于我想要的,这样的按钮实在太大了,尤其是在大屏幕上。
<?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:layout_gravity=\"center_vertical\"
android:gravity=\"center\">
<Button
android:id=\"@+id/button_1\"
android:layout_height=\"fill_parent\"
android:layout_width=\"0dip\"
android:layout_weight=\"1\"
android:text=\"ABCDEF\" />
<Button
android:id=\"@+id/button_2\"
android:layout_height=\"fill_parent\"
android:layout_width=\"0dip\"
android:layout_weight=\"1\"
android:text=\"GHI\" />
<Button
android:id=\"@+id/button_3\"
android:layout_height=\"fill_parent\"
android:layout_width=\"0dip\"
android:layout_weight=\"1\"
android:text=\"JKLM\" />
</LinearLayout>
布局权重1按钮填充屏幕宽度:
-
在父级LinearLayout中为weightSum设置不同的值可用于阻止按钮填充整个屏幕,但是我不认为这是我要走的路,因为我不希望按钮占据一个大屏幕设备上的大部分屏幕。为了澄清,使用weightSum,例如,我可以将三个按钮设置为共同占据屏幕宽度的一半,在小屏幕上看起来不错,但是在大屏幕上,按钮仍将占据屏幕宽度的一半,并且按钮只会比我想要的大得多。也许最终的解决方案将是简单地为不同的屏幕使用不同的布局文件,但是我宁愿不走这条路。
<?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:layout_gravity=\"center_vertical\"
android:gravity=\"center\"
android:weightSum=\"5\">
<Button
android:id=\"@+id/button_1\"
android:layout_height=\"fill_parent\"
android:layout_width=\"0dip\"
android:layout_weight=\"1\"
android:text=\"ABCDEF\" />
<Button
android:id=\"@+id/button_2\"
android:layout_height=\"fill_parent\"
android:layout_width=\"0dip\"
android:layout_weight=\"1\"
android:text=\"GHI\" />
<Button
android:id=\"@+id/button_3\"
android:layout_height=\"fill_parent\"
android:layout_width=\"0dip\"
android:layout_weight=\"1\"
android:text=\"JKLM\" />
</LinearLayout>
重量总和5小屏:
重量总和5大屏幕:
-
我还使用TableLayout尝试了很多事情,但是没有什么比仅使用LinearLayout更好的了。
GridView使用起来非常笨拙,但是我还没有尝试过。
那么,如何创建具有相等宽度的按钮,最好是在仅具有必要宽度的情况下,才能使按钮内容最长的标签适合呢?
任何建议表示赞赏。
(我确实进行了搜索,发现这个问题问了很多遍,但是我发现的所有答案都没有解决我想要达到的目标。)
解决方法
也许最终的解决方案将是简单地为不同的屏幕使用不同的布局文件,但是我宁愿不走这条路。
许多程序员将使用
res/layout/
和res/layout-large/
处理此类情况。在三个按钮的有限情况下,您可能有其他选择,但是通常用户界面并没有那么简单。
那么,如何创建具有相等宽度的按钮,最好是在仅具有必要宽度的情况下,才能使按钮内容最长的标签适合呢?
为了满足您的“最好”的要求,您需要为此创建一个自定义布局类。对于仪表板模式,这是与之相关的一种,您可以将其用作起点。
,如果您预先知道最宽的按钮文字是什么,则可以使用android:ems
属性将按钮设置为该宽度。
<Button
android:id=\"@+id/my_button\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:minEms=\"5\"
android:text=\"@string/my_button\" />
它不是完美的,但这是我发现实现这种外观而又不间断地摆弄布局的最简单方法。
,使用仪表板片段类
http://code.google.com/p/iosched/source/browse/android/src/com/google/android/apps/iosched/ui/DashboardFragment.java
,我这边只有一张纸条!
如果您需要设置在运行时添加的按钮(或其他任何具有简单代码更改的视图)的宽度(或高度),则可以使用下面的代码(不取决于方向):
public void AlignButtons(){
llModules = (LinearLayout) findViewById(R.id.llModules);
ViewTreeObserver viewTree = llModules.getViewTreeObserver();
viewTree.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
int childcount = llModules.getChildCount();
int maxWidth = 0; //int maxHeight = 0;
String fontPath = \"fonts/Isabella-Decor.ttf\";
Typeface tf = Typeface.createFromAsset(getAssets(),fontPath);
for (int i = 0; i < childcount; i++) {
LinearLayout v = (LinearLayout)llModules.getChildAt(i);
View vv = v.getChildAt(0);
if (vv instanceof Button) {
int width = vv.getMeasuredWidth();
maxWidth = (maxWidth > width) ? maxWidth : width;
//int height = vv.getMeasuredHeight();
//maxHeight = (maxHeight > height) ? maxHeight : height;
}
}
for (int i = 0; i < childcount; i++) {
LinearLayout v = (LinearLayout)llModules.getChildAt(i);
View vv = v.getChildAt(0);
if (vv instanceof Button) {
LayoutParams params = ((Button) vv).getLayoutParams();
params.width = maxWidth;
//params.height = maxHeight;
((Button) vv).setLayoutParams(params);
// Applying font
((Button) vv).setTypeface(tf);
}
vv = v.getChildAt(1);
if (vv instanceof TextView) {
// Applying font
((TextView) vv).setTypeface(tf);
}
}
return true;
}
});
}
就我而言:
llModule-一些布局,其中包含我们需要对齐的其他布局(v.getChildAt(0);
)和其他布局(v.getChildAt(1);
)。
LinearLayout v =(LinearLayout)llModules.getChildAt(i); -获取要对齐的按钮的布局。
其他部分很容易理解。
这段代码中的两个周期是完全相同的,但是我仍然无法想象如何将它们组合在一起(以加快执行时间)。
,使用此正确的代码将为您提供帮助。
<LinearLayout
xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:orientation=\"horizontal\"
android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\"
android:gravity=\"center\"
android:weightSum=\"3\">
<Button
android:id=\"@+id/button1\"
android:layout_width=\"0dp\"
android:layout_weight=\"1\"
android:layout_height=\"wrap_content\"
android:text=\"Get the Time\"
android:onClick=\"showNewDate\" />
<Button
android:id=\"@+id/button2\"
android:layout_width=\"0dp\"
android:layout_weight=\"1\"
android:layout_height=\"wrap_content\"
android:text=\"Get the Time\"
android:onClick=\"showNewDate\" />
<Button
android:id=\"@+id/button3\"
android:layout_width=\"0dp\"
android:layout_weight=\"1\"
android:layout_height=\"wrap_content\"
android:text=\"Get the Time\"
android:onClick=\"showNewDate\" />
,将各个按钮保留在其自己的RelativeLayout下。
<LinearLayout
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:orientation=\"horizontal\">
<RelativeLayout
android:layout_width=\"wrap_content\"
android:layout_height=\"match_parent\"
android:layout_weight=\"1\">
<Button
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:layout_alignParentStart=\"true\"
android:text=\"Button1\"/>
</RelativeLayout>
<RelativeLayout
android:layout_width=\"wrap_content\"
android:layout_height=\"match_parent\"
android:layout_weight=\"1\">
<Button
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:layout_alignParentEnd=\"true\"
android:text=\"Button2\"/>
</RelativeLayout>
</LinearLayout>
,读取该线程,然后进行一些反复试验,发现ѭ12是一个可接受的参数。现在,正如我所说,我偶然发现了这个问题,没有尝试使用它来解决您的三键方案。
自从您最初发布以来,情况很可能发生了变化。虽然我在为1.5版进行构建时尝试过此操作。那已经够老了... :-)
这是完整的main.xml
:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout
xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:orientation=\"vertical\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\">
<TextView
android:id=\"@+id/dateText\"
android:text=\"\\n\\nClick for Date\\n\\n\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\" />
<Button
android:id=\"@+id/button\"
android:layout_width=\"180px\"
android:layout_height=\"wrap_content\"
android:layout_gravity=\"center_horizontal\"
android:text=\"Get the Time\"
android:onClick=\"showNewDate\" />
</LinearLayout>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。