【问题标题】:How to set layout Background programatically如何以编程方式设置布局背景
【发布时间】:2014-11-11 12:52:49
【问题描述】:

我想以编程方式设置布局背景,具体取决于要在我的应用程序中播放的歌曲的类型。 我试过这个:

public class AnswerActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("TRANSITION", "TRANSITIONED TO ANSWER ACTIVITY");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play); 
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        LinearLayout root = (LinearLayout) findViewById(R.id.ctr1);
        Bundle data = getIntent().getExtras();
        if(data.getString("genre").equals("rock")){
            root.setBackgroundResource(R.drawable.wprock);
        }
        else if(data.getString("genre").equals("pop")){
            root.setBackgroundResource(R.drawable.wppop);
        }
        else if(data.getString("genre").equals("hiphop")){
            root.setBackgroundResource(R.drawable.wphiphop);
        }

    }

但它不起作用,它会在 root.setBackgroundResource 行中抛出一个空指针异常,只要其中任何一个发生。 有任何想法吗? 提前致谢 编辑:我确实有 R.drawable.wprock/pop/hiphop,而且我排除了这种可能性,因为我尝试使用颜色而不是 setBackgroundColor 方法,我有同样的例外。

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#1d1d1d" >

    <ImageView
        android:id="@+id/res"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"

        android:layout_marginTop="130dp"
        android:onClick="go"
        android:background="@drawable/playbtn" />

</LinearLayout>

【问题讨论】:

标签: android android-intent background bundle


【解决方案1】:

您收到错误是因为您的 root 是一个新的 layout,而您的 Activity 中还没有:

LinearLayout root = new LinearLayout(this);

删除上面的代码,给R.layout.play的外部布局一个android:id,改用findViewbyId

【讨论】:

  • 也试过了,没用,反正我知道最终代码里会有的
  • @user1832697 请发布您的 xml,我也许可以帮助您:)
  • @MD 对不起,如果我犯了错误,但是看看他的代码..他当然会得到 NPE(或者它不起作用,看看我的答案)。我的错误是什么?
  • @MD 和他的问题“如何以编程方式设置布局背景”,我告诉他正确的方法......
  • @MD 谢谢,我明白了。我会尽可能地编辑我的答案。
【解决方案2】:

首先检查 xml 的根布局 id 是否正确,然后按照代码进行操作

protected void onCreate(Bundle savedInstanceState) {
        Log.e("TRANSITION", "TRANSITIONED TO ANSWER ACTIVITY");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play); 
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        LinearLayout root = (LinearLayout) findViewById(R.id.ctr1);
        Bundle data = getIntent().getExtras();
        if(data.getString("genre").equals("rock")){
            root.setBackgroundResource(R.drawable.wprock);
        }
        else if(data.getString("genre").equals("pop")){
            root.setBackgroundResource(R.drawable.wppop);
        }
        else if(data.getString("genre").equals("hiphop")){
            root.setBackgroundResource(R.drawable.wphiphop);
        }

然后检查 Bundle data = getIntent().getExtras(); bundle 中的数据为 null 或其他内容

你的 xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/ctr1" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#1d1d1d" >

<ImageView
    android:id="@+id/res"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"

    android:layout_marginTop="130dp"
    android:onClick="go"
    android:background="@drawable/playbtn" /></LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 2014-10-25
    • 1970-01-01
    相关资源
    最近更新 更多