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

android – 在ImageButton中更改背景位置重力,第1张

概述我有下一个观点: 我希望它看起来像这样: 在第一个中,绿色ImageButton的背景位于左上角. 在第二个中,绿色ImageButton的背景位于中心. (背景图片的大小为绿色方块) 这是我的XML代码: <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com 我有下一个观点:

我希望它看起来像这样:

在第一个中,绿色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); ...
解决方法 您正在以一种过于复杂的方式执行此 *** 作,这只能通过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中更改背景位置/重力全部内容,希望文章能够帮你解决android – 在ImageButton中更改背景位置/重力所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1126957.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存