我正在开发一个Android应用程序,当我为该应用程序设置背景图像时,任何像button或edittext这样的东西也包含该背景,使它们很难看到。
以下是我的代码
MainActivity.java
public class MainActivity extends Activity {
Button b1,b2,b3;
ImageView i1;
int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button1);
View background = findViewById(R.id.background);
background.setBackgroundResource(R.drawable.img2);
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/background"
android:background="@drawable/img2"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_alignTop="@+id/imageView1"
android:layout_marginLeft="65dp"
android:layout_marginTop="50dp"
android:color="#ffffff"
android:text="HELLO" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
我希望按钮的颜色和编辑文本与纯白色背景时的颜色相同。
答案 0 :(得分:0)
试试这个:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button1);
Drawable buttonBackground = b1.getBackground();
View background = findViewById(R.id.background);
background.setBackgroundResource(R.drawable.img2);
b1.setBackgroundResource(buttonBackground);
}
这样可行。