android stuidio 安装怎么改插入背景图片的大小

学无止境,天道酬勤
解决android:background背景图片被拉伸问题
ImageView中XML属性src和background的区别:
background会根据ImageView给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸。src是图片内容(前景),bg是背景,可以同时使用。
此外:scaleType只对src起作用;bg可设置透明度,比如在ImageButton中就可以用android:scaleType控制图片的缩放方式
如上所述,background设置的图片会跟View组件给定的长宽比例进行拉伸。举个例子, 36x36 px的图标放在 xhdpi 文件夹中,在854x480(FWVGA,对应hdpi)环境下,按照
xhdpi : hdpi : mdpi: ldip = 2 : 1.5 : 1 : 0.75
的比例计算,在FWVGA下,图标的实际大小应该是 27x27。
但是当我把它放到一个 layout_width = 96px, layout_height = 75px 的 LinearLayout,布局代码如下:
&linearlayout
android:gravity="center"
android:layout_width="96px"
android:layout_height="75px"&
&imagebutton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/toolbar_bg"&
&/imagebutton&&/linearlayout&
实际情况是,我们得到的ImageButton的大小是 33x27,很明显width被拉伸了,这是我们不想看到的情况。
解决方案一:
代码中动态显式设置ImageButton的layout_width和layout_width,如下
LinearLayout.LayoutParams
layoutParam = new
LinearLayout.LayoutParams(27,
layout.addView(imageButton,
layoutParam);
不过,事实上我们并不希望在代码存在“硬编码”的情况。
解决方案二:
在你通过setBackgroundResource()或者在xml设置android:background属性时,将你的background以XML Bitmap的形式定义,如下:
version="1.0"
encoding="utf-8"?--&
xmlns:android=""
android:id="@id/toolbar_bg_bmp"
android:src="@drawable/toolbar_bg"
android:tilemode="disabled"
android:gravity="top"&
调用如下:
imageButton.setBackgroundResource(R.drawable.toolbar_bg_bmp)
若背景图片有多种状态,还可参照toolbar_bg_selector.xml:
version="1.0"
encoding="utf-8"?--&
xmlns:android=""&
android:state_pressed="true"&
android:src="@drawable/toolbar_bg_sel"
android:tilemode="disabled"
android:gravity="top"&
&/bitmap&&/item&
android:src="@drawable/toolbar_bg"
android:tilemode="disabled"
android:gravity="top"&
&/bitmap&&/item&
&/selector&
如此,不管是通过代码方式setBackgroundResource()或XML android:background方式设置背景,均不会产生被拉伸的情况。
没有更多推荐了,android 自定义toast width height 背景图片
自定义toast 宽高大小 背景图片
RelativeLayout layout = (RelativeLayout) getLayoutInflater().inflate(R.layout.layout_custom_toast,null);
((TextView) layout.findViewById(R.id.tvCheckoutWay)).setText("11111");
((TextView) layout.findViewById(R.id.tvPercent)).setText("22222");
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.BOTTOM, 0,(int)getResources().getDimension(R.dimen.spacing_4));
toast.setView(layout);//setting the view of custom toast layout
toast.show();
layout.xml
android:id="@+id/tvPercent"
android:layout_width="wrap_content"
            android:layout_height="wrap_content"
android:gravity="center"
android:text="{100%}"
android:textColor="@color/green_deep"
android:textSize="@dimen/text_size_menu" />
需要注意的是最外一层layout不要放背景图片,不然怎么都改变不了大小和宽高,要改只能在中间这层再改android 怎么再背景图片上添加图片_百度知道
android 怎么再背景图片上添加图片
我有更好的答案
你是指的用代码写吗?用XML还是直接代码,直接代码的话你先创建一个比如说,Linernlayout
la=new LinernLayout(context);然后你给这个主布局设置一个图片,例如la.setBackgounrdforResource(R.darwable.pic1);然后你再写另一个布局Linernlayout
la2=new LinernLayout(context);这个子布局设置一个大小,例如la2.setLayoutParmgs(new Layoutparmgs(400,400));给子布局设置图片la2.setBackgounrdforResource(R.darwable.pic2);然后你将子布局添加进主布局中la.addView(la2);然后设置显示主布局就OK,就可以看到底下一个图片。上面还有个图片setContentView(la);
采纳率:23%
在xml里写background=“@drawxxx/xxx”
为您推荐:
其他类似问题
android的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。18:20 提问
菜鸟求问!在andriod studio中如何使button大小与背景图片大小一致
没有添加背景图片的话,布局能够正常显示,但是增加背景图片再运行就把整个图片都显出来了,导致覆盖了。那么如何使大小与控件大小一致呢?
这是我的代码,我刚开始学
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android=""
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bg"
android:id="@+id/title_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/back_bg"
android:text="Back"/&
android:id="@+id/title_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Title Text"
android:textColor="#fff"
android:textSize="24sp"/&
android:id="@+id/title_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/edit_bg"
android:text="Edit"
android:textColor="#fff"/&
按赞数排序
你把你button的宽高限定一下就好了吧?写成固定值试试
要什么样的效果,可以试试设置宽高,或者换ImageButton
设置一下宽高就好了,button设置wrap_content控件大小会受背景图片影响
限制button 控件的宽高,不要用包裹wrap_content属性
因为你的Button设置的高度和宽度是wrap_content,这样就会根据text设置宽度和高度,在你设置了background后,就会以back_bg为大小设置button大小
解决办法:
1:设置button固定高度
android:id="@+id/title_back"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/back_bg"
android:text="Back"/&
2:代码解决。
LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
layout.addView(button, layoutParam);
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐android studio改变主题字体大小_百度经验
&&&&&&&&&电脑软件android studio改变主题字体大小听语音12345
百度经验:jingyan.baidu.comandroid studio提供的主题是不能修改字体和字体大小的,如果要修改大小就的另存一份自定义的主题了。本经验介绍如何修改Android Stduio主题的字体和字体大小。百度经验:jingyan.baidu.comAndroid Studio百度经验:jingyan.baidu.com1启动Android Studio,菜单&File&-&Setting...&,如图:2在左边的菜单中选择&Editor&-&Colors & Fonts&-&Font&,如图:3右侧可以看到Darcula主题的字体和字号是不能设置的。4点击&Save As ...&按钮,如图:5输入另存的名字,点击”OK“按钮。如本经验的例子:6修改想要的primary font和字号,然后点击OK。如图:7在代码页中就可以看到效果了。END经验内容仅供参考,如果您需解决具体问题(尤其法律、医学等领域),建议您详细咨询相关领域专业人士。作者声明:本篇经验系本人依照真实经历原创,未经许可,谢绝转载。投票(64)已投票(64)有得(0)我有疑问(0)◆◆说说为什么给这篇经验投票吧!我为什么投票...你还可以输入500字◆◆只有签约作者及以上等级才可发有得&你还可以输入1000字◆◆如对这篇经验有疑问,可反馈给作者,经验作者会尽力为您解决!你还可以输入500字相关经验006912热门杂志第1期你不知道的iPad技巧3803次分享第1期win7电脑那些事6656次分享第2期新人玩转百度经验1418次分享第1期Win8.1实用小技巧2661次分享第1期小白装大神1941次分享◆请扫描分享到朋友圈}

我要回帖

更多关于 android stuidio 安装 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信