Android片段未显示文本视图和内部元素

时间:2016-09-17 20:34:11

标签: android android-layout android-fragments android-fragmentactivity android-framelayout

我是android的新手。我创建了一个tablayout和一个带有两个片段的viewpager。我在我创建的Detail Fragment中有元素/项目,在我的measure片段中有一个TextView但是当我在我的手机上运行代码时,没有显示任何内容在我的片段中。我没有我的片段的java代码。在tablayout上可以正常工作。我可以在标签之间切换但是我的片段中没有显示任何内容......为什么会发生这种情况?

我的活动:

 <?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"
        tools:context="com.example.update">




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

                    <!--<include android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                        layout="@layout/toolbar_layout"/>-->

                    <include
                        android:id="@+id/update_screen_toolbar"
                        layout="@layout/toolbar_layout"
                        />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:id="@+id/name_age_gender"
                        android:background="#d3d3d3"
                        android:layout_below="@+id/update_screen_toolbar"/>



                    <android.support.design.widget.TabLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/tabLayout"
                        app:tabMode="fixed"
                        app:tabGravity="fill">

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


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

                <android.support.v4.view.ViewPager
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/viewPager">
                </android.support.v4.view.ViewPager>
    </RelativeLayout>

活动的JAVA代码

package com.example.update;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;



public class UpdateDetail extends AppCompatActivity {

    TabLayout tabLayout;
    ViewPager viewPager;
    ViewPagerAdapter viewPagerAdapter;

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



        tabLayout=(TabLayout)findViewById(R.id.tabLayout);
        viewPager=(ViewPager)findViewById(R.id.viewPager);

        viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());
        viewPagerAdapter.addFragments(new DetailFragment()," DETAIL");
        viewPagerAdapter.addFragments(new MeasureFragment(),"MEASURE");


        viewPager.setAdapter(viewPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }


}
DetailFragment的代码:

<FrameLayout 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="com.example.DetailFragment">

    <!-- TODO: Update blank fragment layout -->
     <Button
            android:id="@+id/expandableButton4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#066da1"
            android:drawableRight="@android:drawable/arrow_down_float"
            android:onClick="expandableButton4"
            android:paddingRight="10dp"
            android:text="Expand/Collapse Android Example"
            android:textColor="#fff" />

        <com.github.aakira.expandablelayout.ExpandableRelativeLayout
            android:id="@+id/expandableLayout4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/expandableButton4"
            android:background="#90066da1"
            android:padding="16dp"
            app:ael_duration="400"
            app:ael_expanded="false"
            app:ael_interpolator="bounce"
            app:ael_orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Implements the expand and collapse by sliding logic for a top or a bottom view in a two children view  or layout or any widgets composition.
                 Implements the expand and collapse by sliding logic for a top or a bottom view in a two children view  or layout or any widgets composition." />
        </com.github.aakira.expandablelayout.ExpandableRelativeLayout>

</FrameLayout>

1 个答案:

答案 0 :(得分:0)

试试这个:

public class FriendshipParentFragment extends Fragment {


@BindView(R.id.tabs)
TabLayout tabs;

@BindView(R.id.viewpager)
ViewPager viewPager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_friendship_parent, container, false);
    ButterKnife.bind(this, view);
    initTabs();
    return view;
}


private void initTabs(){

    viewPager.setAdapter(new TabsAdapter(getChildFragmentManager()));
    tabs.setupWithViewPager(viewPager);
    tabs.getTabAt(0).setText("Tab1");
    tabs.getTabAt(1).setText("Tab2");
    tabs.setTabTextColors(ContextCompat.getColor(getActivity(), R.color.colorAccent), ContextCompat.getColor(getActivity(), R.color.colorBottomNavigationPrimary));
    tabs.setSelectedTabIndicatorColor(ContextCompat.getColor(getActivity(), R.color.white));
    tabs.setSelectedTabIndicatorHeight(4);
}


public class TabsAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return new FollowersFragment();
            case 1:
                return new FollowFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 2;
    }
}

}

xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="owo.owocar.driver.OrdersParentFragment">


<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:elevation="4dp"
    app:tabGravity="fill" />

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:id="@+id/viewpager"
    android:layout_height="match_parent"></android.support.v4.view.ViewPager>