在android中调整背景大小时调整按钮大小

时间:2015-09-12 12:13:33

标签: android

我正在研究我的论文,这是一款安卓游戏。所以我的布局中到处都有很多按钮。在我的图形布局中,我使用4.7in WXGA,因为它是与我试图使用的手机兼容的设备配置。但是当我将它安装在另一部具有不同尺寸的手机中时,按钮会移动,并且在背景调整大小时不会调整大小。请帮我说明如何使按钮不会改变其在不同尺寸手机中的位置。

这是我的xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bgethnic">

<RelativeLayout  android:layout_width="625dp"
android:layout_height="303dp"
android:background="@drawable/gameilocosnorte"
android:layout_marginTop="50dp" android:layout_marginLeft="7dp">

<Button
android:id="@+id/btn1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="160dp"
android:background="@drawable/iconbox"
android:layout_marginLeft="530dp"/>

<Button
android:id="@+id/btn01"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="225dp"
android:background="@drawable/iconbox"
android:layout_marginTop="160dp"/>

<Button
android:id="@+id/btn2"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="442dp"
android:background="@drawable/iconbox" android:layout_marginTop="85dp"/>

<Button
android:id="@+id/btn02"
android:layout_width="45dp"
android:layout_height="45dp"
android:background="@drawable/iconbox" android:layout_marginTop="85dp"
android:layout_marginLeft="135dp"/>


<Button
android:id="@+id/btn3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="130dp"
android:layout_marginTop="220dp"
android:background="@drawable/iconbox" />


<Button
android:id="@+id/btn03"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/iconbox"
android:layout_marginLeft="430dp" android:layout_marginTop="220dp"/>

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />

<Button
android:id="@+id/btn4"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/iconbox"
android:layout_marginLeft="340dp" android:layout_marginTop="45dp"/>

<Button
android:id="@+id/btn04"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/iconbox" android:layout_marginLeft="30dp"
android:layout_marginTop="45dp"/>

<Button
android:id="@+id/btn5"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:background="@drawable/iconbox"
android:layout_marginTop="140dp"/>

<Button
android:id="@+id/btn05"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="320dp"
android:background="@drawable/iconbox"
android:layout_marginTop="140dp"/>

</RelativeLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_centerHorizontal="true" android:layout_marginTop="15dp"/>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/iconstarthree"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="10dp" android:layout_marginLeft="10dp"/>

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0 of 1"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="15dp" android:layout_marginLeft="500dp"/>

<Button
android:id="@+id/btnhow"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@drawable/btnqmark"
android:layout_marginLeft="590dp" android:layout_marginTop="10dp"/>

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

更改

<RelativeLayout  android:layout_width="625dp"
android:layout_height="303dp"
android:background="@drawable/gameilocosnorte"
android:layout_marginTop="50dp" android:layout_marginLeft="7dp">

<RelativeLayout  android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gameilocosnorte"
android:layout_marginTop="50dp" android:layout_marginLeft="7dp">

答案 1 :(得分:0)

考虑使用AudioUnitReset(_midiPlayer.instrumentUnit, kAudioUnitScope_Global, 0); android:layout_aboveandroid:layout_below,而不是使用边距放置所有内容。

这会使您的布局更容易维护,因为它允许您移动按钮,以便自动移动其他相关按钮。

您还应该使用android:layout_toRightOf中定义的样式来处理具有相似属性的所有按钮,以避免不必要的代码重复。

相关问题