次のビューがあります。
そして、私はそれを次のようにしたい:
最初のものでは、緑色の ImageButton の背景が左上隅にあります。2 つ目は、緑色の ImageButton の背景が中央にあります。(背景画像は緑色の四角の大きさです)
ここに私のXMLコードがあります:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageButton android:id="@+id/dialer"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btncall"
android:scaleType="centerInside"/>
<Button android:id="@+id/sendSMS"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS"/>
<Button android:id="@+id/four"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:id="@+id/shutDownDevice"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn Off"/>
</RelativeLayout>
どこ
android:background="@drawable/btncall"
次のような btncall.xml を参照します。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/phone_down"
android:state_pressed="true" />
<item android:drawable="@drawable/phone_down"
android:state_focused="true" />
<item android:drawable="@drawable/phone_up" />
</selector>
私はアンドロイドの初心者なので、このビューを作成するためのより良い方法があれば教えてください。
10 倍もたくさん、ライアン。
さらに、これはメイン Java ファイルのコードの一部です。
...
//Get width and Height of screen
Display display = getWindowManager().getDefaultDisplay();
int halfStageWidth = display.getWidth()/2;
int halfStageHeight = display.getHeight()/2;
//Get an instance of the Dialer button
ImageButton btnDialer = (ImageButton) findViewById(R.id.dialer);
Button btnSendSMS = (Button) findViewById(R.id.sendSMS);
btnSendSMS.setWidth(halfStageWidth);
btnSendSMS.setHeight(halfStageHeight);
Button btnShutDownDevice = (Button) findViewById(R.id.shutDownDevice);
btnShutDownDevice.setWidth(halfStageWidth);
btnShutDownDevice.setHeight(halfStageHeight);
Button B4 = (Button) findViewById(R.id.four);
B4.setWidth(halfStageWidth);
B4.setHeight(halfStageHeight); ...