Android Fragment / ViewPager - ViewGroups失去背景颜色

时间:2016-07-08 13:34:19

标签: android android-layout android-fragments android-viewpager

我正在使用带有" Tabbed Activity"的ViewPager。由Android Studio生成。

当我运行应用程序时,正确的布局会加载到正确的片段中,您可以轻松地在片段之间滑动。布局是正确的,所有背景颜色都设置得如此。

不幸的是,只要您滑入另一个片段,所有LayoutsViewGroups)都会失去背景颜色,这意味着它们会变得透明。里面的观点看起来仍然一样。

示例:底部的EditText位于LinearLayout内部,应该是白色(左图)但是变为灰色(右图)。

两个屏幕截图:1)加载时和2)刷卡后

normal buggy

最奇怪的是,如果你滑入一个片段而它失去了背景,如果向左或向右滑动一下,一切都会恢复正常,而不会更改标签。

可能是什么问题?

CODE

适配器

public class PageAdapter extends FragmentPagerAdapter {

    public PageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PageFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 4 total pages.
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "POS: " + position;
    }
}

片段

public class PageFragment extends Fragment {
    private static final String WHICH = "WHICH";

    /**
     * The fragment argument representing the section number for this
     * fragment.
     */

    public PageFragment() {}

    public static PageFragment newInstance(int which){
        PageFragment page = new PageFragment();
        Bundle args = new Bundle();
        args.putInt(WHICH, which);
        page.setArguments(args);
        return page;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView;
        switch(getArguments().getInt(WHICH)){
            case 1:
                rootView = inflater.inflate(R.layout.activity_one, container, false);
                break;
            case 2:
                rootView = inflater.inflate(R.layout.activity_two, container, false);
                break;
            case 3:
                rootView = inflater.inflate(R.layout.activity_four, container, false);
                break;
            case 4:
                rootView = inflater.inflate(R.layout.activity_five, container, false);
                break;
            default:
                rootView = inflater.inflate(R.layout.activity_defautlt, container, false);
                break;
        }
        return rootView;
    }
}

背景不正确的LinearLayout:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/layout_id"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:layout_alignParentLeft="true"
        android:background="@color/white"
        android:gravity="center"
        android:weightSum="1">

        <EditText
            android:layout_width="0dp"
            android:layout_weight=".85"
            android:layout_height="match_parent"
            android:textColor="@color/grey_2"
            style="@style/inputDefault"
            android:minLines="1"
            android:maxLines="5"
            android:gravity="start|center"
            android:paddingStart="@dimen/activity_horizontal_margin_medium"
            android:paddingEnd="@dimen/activity_horizontal_margin_medium"
            android:inputType="textMultiLine"
            android:hint="@string/write_message"
            android:id="@+id/chatroom_editText" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".15"
            android:src="@drawable/ic_send_black_24dp"
            style="@style/btnImage"
            android:id="@+id/chatroom_send_btn"
            android:onClick="sendItem"
            android:background="@color/white"
            android:layout_margin="0dp"
            android:padding="0dp"
            />

</LinearLayout>

<style name="inputDefault">
    <item name="android:layout_marginRight">0dp</item>
    <item name="android:layout_marginLeft">0dp</item>
    <item name="android:layout_marginTop">@dimen/activity_vertical_margin_small</item>
    <item name="android:layout_marginBottom">@dimen/activity_vertical_margin_small</item>
    <item name="android:paddingTop">@dimen/activity_vertical_margin_small</item>
    <item name="android:paddingBottom">@dimen/activity_vertical_margin_small</item>
    <item name="android:background">@color/white</item>
    <item name="android:textColor">@color/grey_2</item>
    <item name="android:textColorHint">@color/grey_3</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textCursorDrawable">@null</item>
    <item name="android:stateListAnimator" tools:targetApi="lollipop">@null</item>
</style>

1 个答案:

答案 0 :(得分:1)

实际上它与您的LinearLayoutTabActivity等无关。这是EditText的常见行为。

如果您希望EditText背景透明或在您的情况下为白色,则无需定义android:background="@color/white"只需将背景设为@null,其余部分就可以了。