Android - FloatingActionButton未显示&旋转器莫名其妙地做了

时间:2016-03-14 14:38:50

标签: android android-layout android-fragments

我已经使用Fragment,TabLayout,ViewPager& amp;将标签集成到我的MainActivity中。我的自定义PageAdapter。

布局应该包含FloatingActionButton但是由于它没有,我尝试将它显式放在tab1.xml文件中,但它也没有帮助。 此外,Spinner对象(之前使用过)仍会出现,而在任何布局中都没有定义。

activity_main.xml中

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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" android:fitsSystemWindows="true"
    tools:context=".Main2Activity">

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"


    android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar"


        android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

        android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_input_add"
    android:onClick="newTaskButtonClick"/>

</android.support.design.widget.CoordinatorLayout>

Content_MainActivity.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="il.ac.shenkar.david.todolistex2.Main2Activity"
tools:showIn="@layout/activity_main2">

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>

以下是我用于标签的片段: 的 TAB1:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:id="@+id/Main2ActivitylinearLayout3">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="right"
        android:paddingRight="10dp"
        android:text=""
        android:id="@+id/allt_tab_totalTask"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp"
    android:id="@+id/Main2ActivitylinearLayout">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:paddingLeft="5dp"
        android:paddingRight="8dp"
        android:text="Sort:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/sort_array"
        android:id="@+id/sortSpinner"
        android:gravity="center"
        android:textAlignment="center"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:id="@+id/Main2ActivitylinearLayout2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="No Tasks to Display"
        android:layout_marginTop="60dp"
        android:layout_marginLeft="60dp"
        android:id="@+id/alltab_emptylist"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:textStyle="bold" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/alltasks_listView"
        android:layout_centerHorizontal="true" />
</LinearLayout>

TAB2

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:id="@+id/Main2ActivitylinearLayout1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="right"
            android:paddingRight="10dp"
            android:text=""
            android:id="@+id/totalTask"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:id="@+id/Main2ActivitylinearLayout2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="No Tasks to Display"
            android:gravity="center"
            android:layout_marginTop="60dp"
            android:layout_marginLeft="60dp"
            android:id="@+id/emptylist"
            android:layout_centerHorizontal="true"
            android:textStyle="bold" />

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/waitingtasks_listView"
            android:layout_centerHorizontal="true" />
    </LinearLayout>

</RelativeLayout>

如您所见,FloatingActionButton在activity_main.xml中声明,但由于某种原因未显示。 并非Spinner对象在任何XML文件中定义,但确实出现。

onCreate方法

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
itemListAllTasks = dbM.getAllTasks();
        TaskHashList.addTaskList(itemListAllTasks);
    itemListWaitingTasks = dbM.getSortedTasks(Sorting.fromInteger(Sorting.WAITING.ordinal()));
    TaskHashList.addTaskList(itemListWaitingTasks);

    tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("All Tasks"));
    tabLayout.addTab(tabLayout.newTab().setText("Waiting"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PageAdapter adapter = new PageAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            Log.w("changed tab","");
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}

2 个答案:

答案 0 :(得分:1)

FAB应该是last child of coordinator layout。你的xml看起来有点混乱,正确格式化xml并再次尝试运行。

答案 1 :(得分:1)

重新安排'Content_MainActivity.xml'后,这个问题得到了解决。