Android 更改图像中的背景位置/重力按钮

Android 更改图像中的背景位置/重力按钮,android,imagebutton,gravity,Android,Imagebutton,Gravity,我有另一种看法: 我希望它看起来像这样: <?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" /> <

我有另一种看法:

我希望它看起来像这样:

<?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>
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
// Add relativeLayout rules here
btnDialer.setLayoutParams(params);

在第一个窗口中,绿色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>
指的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>
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
// Add relativeLayout rules here
btnDialer.setLayoutParams(params);

您尚未设置imagebutton的大小,因此它会环绕图像。您应该设置大小,并且它应该位于按钮的中心

更新:

您可以设置如下布局大小:

<?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>
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
// Add relativeLayout rules here
btnDialer.setLayoutParams(params);

您使用的方式太复杂了,这只能通过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>


Hi Raz,ImageButton没有setWidth()/setHeight()函数网格布局,而linearlayout可能会更好