Android 如何更改布局的背景
我正在学习android,它很难按照android推荐的xml方式来描述UI和逻辑。我正在努力学习。我需要帮助改变背景 我的布局中有两个图像。一个是HUD,位于左侧,另一个是bg 有很多bg,但现在我刚刚放了一个。我需要帮助如何更改此bg,因为用户将单击一些按钮 在my main1.xml中,有8个按钮,用户将单击对应于main2.xml中bg的按钮 以下是main2.xml的代码Android 如何更改布局的背景,android,android-layout,Android,Android Layout,我正在学习android,它很难按照android推荐的xml方式来描述UI和逻辑。我正在努力学习。我需要帮助改变背景 我的布局中有两个图像。一个是HUD,位于左侧,另一个是bg 有很多bg,但现在我刚刚放了一个。我需要帮助如何更改此bg,因为用户将单击一些按钮 在my main1.xml中,有8个按钮,用户将单击对应于main2.xml中bg的按钮 以下是main2.xml的代码 <?xml version="1.0" encoding="utf-8"?> <!-- Hori
<?xml version="1.0" encoding="utf-8"?>
<!-- Horizontal Layout for HUD and Level Screen -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" >
<!-- Vertical Layout for the HUD which includes Object list,Timer, Pause and Menu Button -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/hud"
android:orientation="vertical" >
</LinearLayout>
<!-- Relative Layout for the Level Screen -->
<RelativeLayout
android:id="@+id/GamePlayScreen"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/bg" >
</RelativeLayout>
</LinearLayout>
这适用于一个bg如何保持相同的布局main2.xml和保持changin bg w.r.t按钮单击您可以尝试
视图.setBackgroundResource(int)
)
我自己没有使用过它,所以我不知道它是否有效在您膨胀视图后,请执行以下操作: 我的观点。退步可拉深(d)
d是可绘制的。像这样创建xml我为4个按钮添加了4个,以满足您的需求
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:id="@+id/linear">
<Button<
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
好的,你想改变哪种活动布局??请在我的上述代码中,即在我的main2.xml相对布局图中,明确说明必须更改的bg。下面是流程图。。我的main1.xml有level1、level2等按钮……根据选择,我必须将bg放在main2.xml的相对布局中,其id为android:background=“@drawable/bg”
package com.capo.searchrestaurant.activities;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
LinearLayout l;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l=(LinearLayout)findViewById(R.id.linear);
Button btn1=(Button)findViewById(R.id.button1);
Button btn2=(Button)findViewById(R.id.button2);
Button btn3=(Button)findViewById(R.id.button3);
Button btn4=(Button)findViewById(R.id.button4);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
//.......
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
l.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));
break;
case R.id.button2:
l.setBackgroundDrawable(getResources().getDrawable(R.drawable.images));
break;
case R.id.button3:
l.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));
break;
case R.id.button4:
l.setBackgroundDrawable(getResources().getDrawable(R.drawable.images));
break;
//....for remaining buttons
}
}
}