现在的位置: 首页 > 综合 > 正文

修改linearlayout背景

2013年02月23日 ⁄ 综合 ⁄ 共 920字 ⁄ 字号 评论关闭

The xml would be like this:

Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/currImage"
android:id="@+id/GameLayout"
/>

The currImage.png or any images reside in the res folder of the Project. 
ProjectName/res/drawable/anyImage.png

You would then change the image of the layout in the java file by getting the resource handle.

Code:
public class mainActivity extends Activity 
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources(); //resource handle
	Drawable drawable = res.getDrawable(R.drawable.newImage); //new Image that was added to the res folder
	LinearLayout linearLayout =   (LinearLayout)findViewById(R.id.GameLayout); 
	linearLayout.setBackgroundDrawable(drawable);
    }
}

I hope that answers your question.

抱歉!评论已关闭.