Java “之间的差距”;“图像”;及;“文本”;一个按钮的尺寸太大了
我已经开发了一个应用程序,下面是代码 这是此应用程序的代码Java “之间的差距”;“图像”;及;“文本”;一个按钮的尺寸太大了,java,android,eclipse,user-interface,android-relativelayout,Java,Android,Eclipse,User Interface,Android Relativelayout,我已经开发了一个应用程序,下面是代码 这是此应用程序的代码 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HomeScreen" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"
android:weightSum="4" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:id="@+id/fBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/fStr" />
<Button
android:id="@+id/rBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/rStr" />
<Button
android:id="@+id/sBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/sStr" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:id="@+id/cBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/cStr" />
<Button
android:id="@+id/aBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/aStr" />
<Button
android:id="@+id/lBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/lStr" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:id="@+id/oBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/oStr" />
<Button
android:id="@+id/tBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/tStr" />
<Button
android:id="@+id/eBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/eStr" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:id="@+id/dBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/dStr" />
<Button
android:id="@+id/hBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/hStr" />
<Button
android:id="@+id/aaBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/aStr" />
</TableRow>
</TableLayout>
</RelativeLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="fStr">Fffffffff</string>
<string name="rStr">Rrrrrr</string>
<string name="sStr">Ssssss </string>
<string name="lStr">Lllll</string>
<string name="oStr">Oooooooooooo</string>
<string name="tStr">Ttttt</string>
<string name="eStr">Eeeeee</string>
<string name="dStr">Dddddddddd</string>
<string name="cStr">Cccccccc</string>
<string name="hStr">Hhhh</string>
<string name="aStr">Aaaaaaaaaa aaaa</string>
</resources>
Fffffffff
RRRR
SSSS
微光
呜呜呜呜
Ttttt
Eeeee
Dddddddddd
中交
啊
aaaaaaaaaaaa
此应用程序存在问题
如何解决此问题?可能使用线性布局
![<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HomeScreen" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="0.33"
android:orientation="horizontal">
<Button
android:id="@+id/fBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight="0.33"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/fStr" />
<Button
android:id="@+id/rBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight="0.33"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/rStr" />
<Button
android:id="@+id/sBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight="0.33"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/sStr" />
</LinearLayout>
<LinearLayout
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="0.33"
android:orientation="horizontal">
<Button
android:id="@+id/fBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight="0.33"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/fStr" />
<Button
android:id="@+id/rBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight="0.33"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/rStr" />
<Button
android:id="@+id/sBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight="0.33"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/sStr" />
</LinearLayout>
<LinearLayout
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="0.33"
android:orientation="horizontal">
<Button
android:id="@+id/fBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight="0.33"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/fStr" />
<Button
android:id="@+id/rBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight="0.33"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/rStr" />
<Button
android:id="@+id/sBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight="0.33"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/sStr" />
</LinearLayout>
<LinearLayout
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="0.33"
android:orientation="horizontal">
<Button
android:id="@+id/fBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight="0.33"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/fStr" />
<Button
android:id="@+id/rBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight="0.33"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/rStr" />
<Button
android:id="@+id/sBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight="0.33"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/sStr" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
![
尝试使用android:drawablePadding、android:paddingLeft和android:paddingRight
请参阅这里有一个修改版本,它将缩小按钮的大小,并将它们放在行的中心,并去掉无用的RelativeLayout父级(正如Lint可能向您指出的那样):
我以前遇到过这个问题,我解决这个问题的方法是为每个项目提供一个特定的imageview和textview,包装在一个布局中(对我来说是线性的)并将onclick事件放在布局上。还有一个drawablePadding attr可能会有所帮助also@TomHart:可拉伸填充不起作用。是的,对我来说也不起作用,这就是为什么我需要为每个按钮设置单独的布局。
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:stretchColumns="*"
android:weightSum="4"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HomeScreen">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
<Button
android:id="@+id/fBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher"
android:layout_gravity="center_vertical"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="bla" />
<Button
android:id="@+id/rBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher"
android:layout_gravity="center_vertical"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="bla" />
<Button
android:id="@+id/sBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher"
android:layout_gravity="center_vertical"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="bla" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
<Button
android:id="@+id/cBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher"
android:layout_gravity="center_vertical"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="bla" />
<Button
android:id="@+id/aBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher"
android:layout_gravity="center_vertical"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="bla" />
<Button
android:id="@+id/lBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher"
android:layout_gravity="center_vertical"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="bla" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
<Button
android:id="@+id/oBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher"
android:layout_gravity="center_vertical"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="bla" />
<Button
android:id="@+id/tBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher"
android:layout_gravity="center_vertical"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="bla" />
<Button
android:id="@+id/eBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher"
android:layout_gravity="center_vertical"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="bla" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
<Button
android:id="@+id/dBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher"
android:layout_gravity="center_vertical"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="bla" />
<Button
android:id="@+id/hBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher"
android:layout_gravity="center_vertical"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="bla" />
<Button
android:id="@+id/aaBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher"
android:layout_weight=".3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="bla" />
</TableRow>
</TableLayout>