Изменить положение фона / гравитацию в ImageButton - PullRequest
4 голосов
/ 27 октября 2011

У меня есть следующий вид:

enter image description here

И я хочу, чтобы это выглядело так:

enter image description here

ВВо-первых, фон зеленого ImageButton находится в верхнем левом углу.А во втором фоне зеленый 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>

Я новичок в Android, поэтому, если вы видите лучший способ создать этот вид, пожалуйста, скажите мне.

10x много, Райан.


В Addtion это часть кода в основном файле 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); ...

Ответы [ 2 ]

3 голосов
/ 27 октября 2011

Вы делаете это слишком сложным способом, это может быть сделано только с помощью XML без необходимости использования кода Java.

Ваш XML должен быть:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="horizontal" >
    <ImageButton
        android:id="@+id/dialer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/btncall"
        android:background="@android:color/transparent"
        android:scaleType="centerInside" 
        android:layout_weight="1"/>
    <Button
        android:id="@+id/sendSMS"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Send SMS"
        android:layout_weight="1" />
</LinearLayout>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="horizontal" >
    <Button
        android:id="@+id/four"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_weight="1"/>
    <Button
        android:id="@+id/shutDownDevice"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Turn Off" 
        android:layout_weight="1"/>
</LinearLayout>

0 голосов
/ 27 октября 2011

Вы не установили размер кнопки изображения, чтобы она оборачивалась вокруг изображения.Вы должны установить размер, и он должен центрироваться внутри кнопки.

Обновление:

Вы можете установить размер макета следующим образом:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
// Add relativeLayout rules here
btnDialer.setLayoutParams(params);
...