Ger*_*ard 8 layout android button
我使用一个简单的水平LinearLayout将3个按钮放在一起,宽度相同.我通过以下xml文本实现此目的:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:baselineAligned="false" >
<Button
android:id="@+id/help"
style="android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/help" />
<Button
android:id="@+id/close"
style="android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/closemultigame" />
<Button
android:id="@+id/ok"
style="android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/restartmultigame" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
现在布局看起来像这样:

按钮layout_height定义为wrap_content,但最右边的按钮不包含其内容.它在不同的设备上看起来不同,有些看起来不错.我实际想要实现的是三个按钮,相同的宽度和相同的高度,每个按钮的文本水平和垂直居中.你会提出什么方法?
增加:按要求的整个布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:drawableLeft="@drawable/matchandfit"
android:drawablePadding="@dimen/activity_horizontal_margin"
android:gravity="center_vertical"
android:padding="@dimen/activity_horizontal_margin"
android:text="@string/multigameover"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/multigameresult"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:id="@+id/masterresult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/masterresultinfo"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:baselineAligned="false" >
<Button
android:id="@+id/help1"
style="android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/help" />
<Button
android:id="@+id/close1"
style="android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/closemultigame" />
<Button
android:id="@+id/ok1"
style="android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/restartmultigame" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
正如Wasim Ahmed指出的那样,这是由于ScrollView.现在我使用ScrollView确保按钮在小型设备上即使在横向上也可以访问.(布局用于对话框).有没有其他方法可以始终保持按钮可访问/可见?
解决方法:我发现的解决方案或更好的解决方法是在封闭的LinearLayout的末尾添加TextView.这个TextView在其高度的大约一半处被垂直剪裁,但它不包含任何文本,因此没有人会注意到.在封闭的LinearLayout底部添加一些填充没有帮助
一次又一次,我观察到的Button类不即使您指定调整文字大小android:layout_height="wrap_content"。
您可以执行以下两项操作之一:
1.您可以手动设置文字大小或宽度,Button直到文字适合(模拟 的外观wrap_content)。
2.使用TextView替代(当你指定哪个不换行文本正确android:layout_height="wrap_content"),你设置onClickListener的TextView模拟按钮的行为。
按钮minHeight默认有。minHeight像这样删除Button:
android:minHeight="0dp"
Run Code Online (Sandbox Code Playgroud)
我刚刚尝试过并且有效:
1)在../res/values/strings.xml中定义:
线路1线路1\n线路2线路2
2)在布局文件中引用它:
<Button
android:id="@+id/btn_multilines"
android:text="@string/multilines"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</Button>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9780 次 |
| 最近记录: |