android - 在 ImageButton 中更改背景位置/重力

标签 android imagebutton gravity

我有下一个观点:

enter image description here

我希望它看起来像这样:

enter image description here

在第一个中,绿色 ImageButton 的背景位于左上角。 在第二个中,绿色 ImageButton 的背景位于中央。 (背景图片以绿色方 block 大小为准)

这是我的 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 的初学者,所以如果您看到更好的方法来创建这个 wiew,请告诉我。

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); ...

最佳答案

您正在以一种过于复杂的方式进行操作,这只能通过 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>

关于android - 在 ImageButton 中更改背景位置/重力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7909423/

相关文章:

android - 自定义 View 上的手势检测器

Android ImageButton 削减图标顶部

android - 更改小部件中的图像

android - 使用 Android 检测人的跌倒

ios - 使用 SpriteKit 更改特定节点上的引力

android - 未找到心率传感器 (LG Watch Urbane)

android - 在 Android 中从互联网获取数据

android - 划分我的 cardview 设计问题

android - 将两个动画应用于同一 View ?

android - 通过java为android中的RemoteView设置重力