更改片段内视图的背景颜色

时间:2014-03-10 17:50:26

标签: android xml user-interface android-fragments

好的,这是我的新问题;

我正在尝试更改触摸事件中片段内视图的背景颜色。

它已经设置好了。但似乎背景不会改变颜色。

我确实发生了这种情况,因为此视图在填充父级上也有其他视图因此,即使它改变了颜色,我也无法看到它。

无论如何,我已经将这些视图颜色设置为Color.TRANSPARENT,但它仍然不起作用......

这是我的xml文件:

<RelativeLayout
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/lay_1">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/position"
    android:id="@+id/pos"/>


<ViewFlipper
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/pos"
    android:id="@+id/flipper_1"
    >

    <include layout="@layout/cart"/>

    <include layout="@layout/ang"/>

    <include layout="@layout/data_graph"/>

</ViewFlipper>

如果您需要更多信息,请不要介意。

非常感谢。

路易斯。

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,您应该尝试创建一个在片段的oncreate中调用的方法来更新片段视图的内部(背景颜色,文本......) 例如: public static LinearLayout getcolor(LayoutInflater inflater,ViewGroup container,boolean bf){

int i;
View v;
TextView t;
LinearLayout ll;

int r = (int)Math.round(Math.random()*255+1);
int g = (int)Math.round(Math.random()*255+1);
int b = (int)Math.round(Math.random()*255+1);

i=Color.argb(200,r,g,b);
if(bf){
    ll = (LinearLayout )inflater.inflate(R.layout.fragment_card_front, container, false);
    v=ll.findViewById(R.id.frag_card_front);
    v.setBackgroundColor(i);
    t=(TextView) ll.findViewById(R.id.textf);
    t.setText(""+i);
}
else{
        ll = (LinearLayout )inflater.inflate(R.layout.fragment_card_back, container, false);
        v=ll.findViewById(R.id.frag_card_back);
        v.setBackgroundColor(i);
        t=(TextView) ll.findViewById(R.id.textb);
        t.setText(""+i);

}

return ll;

}


public static class CardFrontFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        LinearLayout ll = (LinearLayout ) getcolor(inflater, container,true);
     return ll;
    }
}


public static class CardBackFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        LinearLayout ll = (LinearLayout ) getcolor(inflater, container,false);      
        return ll;
    }
}
相关问题