XML图片更改

时间:2016-01-19 15:53:25

标签: java android xml image background

我想在按下按钮时更改我的xml代码。这可能吗?

所以,当我按下我想要的按钮时

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/steg"
    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:orientation="vertical"
    android:gravity="bottom">

更改为android:background =&#34; @ drawable / test&#34;

感谢您的帮助

JK

我尝试了你的解决方案但没有工作:(

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageButton;
import android.widget.LinearLayout;

public class einstellungen extends Fragment {

    ImageButton btn;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.einstellungen, container, false);


        btn = (ImageButton)rootView.findViewById(R.id.boote);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((LinearLayout)findViewById(R.id.headerLayout)).setBackgroundDrawable(getResources().getDrawable(R.drawable.kapelle));
            }
        });

        return rootView;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

这是我的课可以有人帮助我吗?

3 个答案:

答案 0 :(得分:1)

试试这个`

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/headerLayout"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/steg"
    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:orientation="vertical"
    android:gravity="bottom">

`在你班上

((LinearLayout)findViewById(R.id.headerLayout)).setBackgroundDrawable(getResources().getDrawable(R.drawable.test));

答案 1 :(得分:0)

您可以通过编程方式执行此操作:

myButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        myLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.test));
    }
});

答案 2 :(得分:0)

为您的布局提供ID

    public interface INormalizedObject
    {
     void someMethodThatHappensToNormalizedObjects();
     T NormalizeObject<T>(T obj) where T : INormalizedObject;
    }

然后通过

获取布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/yourLayoutID"
android:layout_width="match_parent"
....
/>

然后您可以使用

设置背景
LinearLayout layout =(LinearLayout)findViewById(R.id.yourLayoutID);