如何在视图中更改矩形的背景颜色?

时间:2015-03-26 18:02:40

标签: java android xml

我想动态更改矩形的颜色。正常的技术就像 查看v =(查看)findViewById(R.id.myRectangleView);不管用?有什么想法解决它?

这是我的drawable / rectangle.xml

    <?xml version="1.0" encoding="UTF-8"?>
  <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:top="-1dp">
        <shape android:shape="rectangle"  android:id="@+id/table_foreground_shape">
            <stroke android:width="1dp"
                    android:color="#000000" />

            <padding android:left="2dp"
                     android:top="2dp"
                     android:right="2dp"
                     android:bottom="2dp" />
            <corners android:radius="0dp" />
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
  </layer-list>

这是我的displayfragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">
    <View
        android:id="@+id/myRectangleView"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:background="@drawable/rectangle"/>
    </RelativeLayout>

这是我的Display.java

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

public class DisplayFrag extends Fragment {
     private View v;
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle
            savedInstanceState) {

        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.display_frag,
                container, false);

        v=(View)view.findViewById(R.id.myRectangleView);

        return view;
    }
    public void changeBackgroundColor(int color)    {
        v.setBackgroundColor(color);
    }

}

编辑: 这是我的mainActivity.java

public class MainActivity extends ActionBarActivity {
     private DisplayFrag displayFrag;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         displayFrag=(DisplayFrag)getSupportFragmentManager().findFragmentById(R.id.display_fragment);

        if(background()){
           displayFrag.changeBackgroundColor(0);

        }
        else{
              displayFrag.changeBackgroundColor(1);

        }

    }

}

1 个答案:

答案 0 :(得分:0)

看起来好像你没有将你的布局提交给fragmentmanager。

public abstract class SingleFragmentActivity extends FragmentActivity {

    protected abstract Fragment createFragment();

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

        setContentView(R.layout.activity_main);
        FragmentManager manager = getSupportFragmentManager();
        Fragment fragment = manager.findFragmentById(R.id.fragmentContainer);

        if(fragment == null){
            fragment = createFragment();
            manager.beginTransaction().add(R.id.fragmentContainer, fragment).commit();
        }
    }
}

我想把它变成一个评论,但我的代表不够高。 祝你好运。