繁体   English   中英

无法更改片段的背景颜色

[英]Unable to change background color of a fragment

我是android新手,目前正在学习。

我遇到了与android片段有关的问题,我编写了代码,但没有执行任何操作! 只需显示片段颜色即可。

当我尝试去除片段的颜色时,它起作用了! 我无法纠正此问题。 请帮忙!

XML -activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="113dp"
        android:layout_height="50dp"
        android:text="Switch1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.945" />

    <fragment
        android:id="@+id/fragment"
        android:name="com.example.fragments.Color_Purple"
        android:layout_width="390dp"
        android:layout_height="385dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.68"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.137" />


</androidx.constraintlayout.widget.ConstraintLayout>

XML-片段代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Color_Purple">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#C403FA" />

</FrameLayout>

JAVA-mainActivity.java

package com.example.fragments;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button b1;
    Fragment f1;
    Boolean flag=true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1=(Button)findViewById(R.id.button);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                f1=new Color_Purple();
               // Toast.makeText(getApplicationContext(),"Button pressed",Toast.LENGTH_SHORT).show();

                FragmentManager fm=getSupportFragmentManager();
                FragmentTransaction ft=fm.beginTransaction();
                if(flag)
                {
                    findViewById(R.id.fragment).setBackgroundColor(Color.GREEN);
                   // Toast.makeText(getApplicationContext(),"Button pressed Color GREEN",Toast.LENGTH_SHORT).show();
                    flag=false;
                }
                else
                {
                    findViewById(R.id.fragment).setBackgroundColor(Color.RED);
                    //Toast.makeText(getApplicationContext(),"Button pressed Color RED",Toast.LENGTH_SHORT).show();
                    flag=true;
                }
            }
        });
    }
}

Java-片段Java代码

package com.example.fragments;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Color_Purple extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_color__purple, container, false);

    }

}

注意:

如果我从xml-片段中删除android:background,则此代码有效,但不能使用颜色。

只是改变 :

Boolean flag = true;

 boolean flag = true;

并从您的TextView删除android:background="#C403FA"

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#C403FA" //remove this line
        />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM