Programing

프로그래밍 방식으로 활동의 배경색을 흰색으로 설정하는 방법은 무엇입니까?

lottogame 2020. 7. 9. 08:19
반응형

프로그래밍 방식으로 활동의 배경색을 흰색으로 설정하는 방법은 무엇입니까?


프로그래밍 방식으로 활동의 배경색을 흰색으로 설정하려면 어떻게합니까?


사용 된 루트 레이아웃에 대한 핸들을 얻은 다음 배경색을 설정하십시오. 루트 레이아웃은 setContentView라고하는 것입니다.

 setContentView(R.layout.main);

  // Now get a handle to any View contained 
  // within the main layout you are using
  View someView = findViewById(R.id.randomViewInMainLayout);

  // Find the root view
  View root = someView.getRootView();

  // Set the color
  root.setBackgroundColor(getResources().getColor(android.R.color.red));

setContentView()통화 후 활동에이 한 줄 추가

getWindow().getDecorView().setBackgroundColor(Color.WHITE);

테마별로 채색하는 것을 선호합니다

<style name="CustomTheme" parent="android:Theme.Light">
    <item name="android:windowBackground">@color/custom_theme_color</item>
    <item name="android:colorBackground">@color/custom_theme_color</item>
</style>

?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="#FFFFFF"
android:id="@+id/myScreen"
</LinearLayout>

즉, "android : background"는 변경하려는 XML의 태그입니다.

백그라운드 값을 동적으로 업데이트해야하는 경우 다음을 참조하십시오.

연습 : SeekBar로 배경색 변경


이를 사용하여 사전 정의 된 Android 색상을 호출 할 수 있습니다.

element.setBackgroundColor(android.R.color.red);

고유 한 사용자 정의 색상 중 하나를 사용하려면 strings.xml에 사용자 정의 색상을 추가 한 후 아래를 사용하여 호출하십시오.

element.setBackgroundColor(R.color.mycolour);

그러나 layout.xml에서 색상을 설정하려면 아래를 수정하여 허용하는 모든 요소에 아래를 추가하십시오.

android:background="#FFFFFF"

당신의 onCreate()방법에서 :

getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.main_activity_background_color));

또한 값 폴더에 새 XML 파일이라는 새 폴더를 추가 color.xml하고 새 색 속성을 지정해야합니다.

color.xml :

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="main_activity_background_color">#000000</color>
</resources>

color.xml원하는 이름을 지정할 수 있지만 코드에서는이라고 R.color.yourId합니다.

편집하다

때문에 getResources().getColor()사용되지 않으며, 사용하는 getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.main_activity_background_color)); 대신.


작업 표시 줄없이 xml 파일에 정의 된 루트보기를 얻으려면 다음을 사용할 수 있습니다.

View root = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);

따라서 색상을 흰색으로 변경하려면

root.setBackgroundResource(Color.WHITE);

View randview = new View(getBaseContext());
randview = (View)findViewById(R.id.container);
randview.setBackgroundColor(Color.BLUE);

나를 위해 일했다. 감사합니다.


final View rootView = findViewById(android.R.id.content);
rootView.setBackgroundResource(...);

Button btn;
View root;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button)findViewById(R.id.button);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            root =findViewById(R.id.activity_main).getRootView();
            root.setBackgroundColor(Color.parseColor("#FFFFFF"));
        }
    });
}

나는 비슷한 문제를 겪고 있었고 위에 주어진 답변에서 언급 된 단계를 따랐습니다. 나는 나를 위해 일한 것을 공유 할 것입니다.

I was using a RecyclerView, and had set a background color for the item. However, it looked weird when the number of items didn't fit the screen

My initial layout

I then used the function

public void setActivityBackgroundColor(int color) {
        View view = this.getWindow().getDecorView();
        view.setBackgroundColor(color);
    }

And called the function setActivityBackgroundColor(R.color.colorAccent); in my code in another function. That led to this layout. Pretty weird.

I finally fixed it by following what KitKat suggested.

public void setActivityBackgroundColor(int color) {
        View view = this.getWindow().getDecorView();
        view.setBackgroundResource(color);
    }

This finally fixed it.

EDIT: The solution started throwing an error if the RecyclerView was not populated. In that case, I set a flag to check if the view is empty or not.

 public void setActivityBackgroundColor(int color) {
        View view = this.getWindow().getDecorView();
        if (dataIsNull) {
            view.setBackgroundColor(color);
        }
        else {
            view.setBackgroundResource(color);
        }
    }

It works like a charm now.

참고URL : https://stackoverflow.com/questions/4761686/how-to-set-background-color-of-an-activity-to-white-programmatically

반응형