【问题标题】:Change background position/gravity in ImageButton在 ImageButton 中更改背景位置/重力
【发布时间】:2011-10-26 21:56:51
【问题描述】:

我有下一个观点:

我希望它看起来像这样:

在第一个中,绿色 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 的初学者,所以如果你看到创建这个 wiew 的更好方法,请告诉我。

10 倍很多, 瑞恩。


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

【问题讨论】:

    标签: android imagebutton gravity


    【解决方案1】:

    你这样做的方式太复杂了,这只能通过 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>
    

    【讨论】:

    • 网格布局而不是线性布局可能会更好
    【解决方案2】:

    您尚未设置图像按钮的大小,因此它环绕图像。你应该设置大小,它应该在按钮内居中。

    更新:

    您可以像这样设置布局大小:

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

    【讨论】:

    • 嗨 Raz,ImageButton 没有 setWidth()/setHeight() 函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多