没有添加背景图片的话,布局能够正常显示,但是增加背景图片再运行就把整个图片都显出来了,导致覆盖了。那么如何使大小与控件大小一致呢?
这是我的代码,我刚开始学
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bg"
>
<Button
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"/>
<TextView
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"/>
<Button
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, 50);
layout.addView(button, layoutParam);