Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 碎片在活动中占据了全屏_Android_Android Layout_Android Fragments - Fatal编程技术网

Android 碎片在活动中占据了全屏

Android 碎片在活动中占据了全屏,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,我被困在这一点上,在NavigationDrawer活动中添加片段。当活动显示片段时,片段会在整个屏幕上显示,如下图所示: 预期行为如下图所示 我正在使用带有RecyclerView的数据绑定StockListItem是用于访问RecyclerView中数据的POJO类,stock\u list\u item\u layout是用于在RecylerView中显示列表项的布局 public class MainActivity extends AppCompatActivity imp

我被困在这一点上,在
NavigationDrawer
活动中添加
片段。当活动显示片段时,片段会在整个屏幕上显示,如下图所示:

预期行为如下图所示

我正在使用带有RecyclerView的数据绑定
StockListItem
是用于访问RecyclerView中数据的POJO类,
stock\u list\u item\u layout
是用于在RecylerView中显示列表项的布局

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
    private RecyclerViewAdapter adapter;
    DrawerLayout drawer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        drawer  = (DrawerLayout) findViewById(R.id.drawer_layout);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, 
    R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) 
    findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        getSupportFragmentManager().beginTransaction().add(R.id.flContainer,new 
    WatchlistFragment()).commit();

    }
}
这就是stock\u list\u item\u layout.xml的外观

<?xml version="1.0" encoding="utf-8"?>
<layout >
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".WatchlistFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content">
            <TextView
                android:text="Watchlist"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rvMyRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>
    </FrameLayout>
</layout>
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            type="com.example.mvp1stockmeter.StockListItem"
            name="Stock"/>
    </data>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foreground="?attr/selectableItemBackground"
        android:layout_margin="4dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/tvStockName"
                android:layout_margin="16dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{Stock.StockName}"
                android:layout_alignParentLeft="true"/>
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:gravity="right">
                <TextView
                    android:id="@+id/tvStockPrice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="2dp"
                    android:text="@{Stock.StockPrice}"
                    android:textSize="20sp"
                    />
                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <TextView
                        android:id="@+id/tvStockPriceChange"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="2dp"
                        android:text="@{Stock.StockPriceChange}"
                        android:textSize="16sp"
                        android:layout_alignParentRight="true"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="2dp"
                        android:text="("
                        android:textSize="16sp"/>
                    <TextView
                        android:id="@+id/tvStockPriceChangePercent"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="2dp"
                        android:text="@{Stock.StockPriceChangePercent}"
                        android:textSize="16sp"
                        />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="2dp"
                        android:text="%)"
                        android:textSize="16sp" />
                </LinearLayout>

            </LinearLayout>

        </RelativeLayout>
    </android.support.v7.widget.CardView>
</layout>
content\u main.xml
文件如下所示-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/app_bar_main"
android:orientation="vertical">


<FrameLayout
    android:visibility="visible"
    android:id="@+id/flContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="?attr/actionBarSize">
</FrameLayout>
</LinearLayout>
这就是我的
activity\u main\u drawer.xm
文件的外观-

<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"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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_main" />

</android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/app_name"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    app:srcCompat="@mipmap/ic_launcher_round" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:text="@string/app_name"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

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

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_watchlist"
        android:icon="@drawable/ic_menu_camera"
        android:title="Watchlist" />

    <item
        android:id="@+id/nav_profile"
        android:icon="@drawable/ic_menu_manage"
        android:title="Profile" />
    <item
        android:id="@+id/nav_suggestions_or_complaints"
        android:icon="@drawable/ic_menu_manage"
        android:title="Suggestions / Complaints" />
    <item
        android:id="@+id/nav_upcoming_features"
        android:icon="@drawable/ic_menu_manage"
        android:title="Upcoming Features" />
</group>
</menu>

只需将android:layout\u marginTop=“?attr/actionBarSize”
添加到您的activity\u main的
FrameLayout
,如下所示:

 <FrameLayout
        android:visibility="visible"
        android:layout_marginTop="?attr/actionBarSize"
        android:id="@+id/flContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </FrameLayout>

只需在您的活动\u main的
框架布局中添加
android:layout\u marginTop=“?attr/actionBarSize”
,如下所示:

 <FrameLayout
        android:visibility="visible"
        android:layout_marginTop="?attr/actionBarSize"
        android:id="@+id/flContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </FrameLayout>


您在哪里添加了工具栏?导航抽屉codeHey@TejasPandya在哪里,您是指
MainActivity.java
中的
NavigationDrawer
onNavigationItemSelected
方法的视图标记吗?@AbhijeetKharatmol您的xml工具栏在哪里?在xml中查看导航抽屉的标记在哪里添加了工具栏?导航抽屉codeHey@TejasPandya在哪里,您是指
MainActivity.java
中的
NavigationDrawer
onNavigationItemSelected
方法的查看标记吗?@AbhijeetKharatmol您的工具栏在xml中的哪里?在xml中查看导航抽屉的标记谢谢您的回复。我刚刚尝试添加了
android:layout_marginTop=“?attr/actionBarSize”
,发现它只是将片段视图向下移动了一点。工具栏仍然不可见。嘿,Suraj,我已经为问题添加了所有必要的文件。Bdw,当我尝试使用其他
片段
时,它只有“Hello World”文本,没有任何花哨的内容,工作正常。只有使用
fragment\u watchlist.xml
这种奇怪的事情才会发生。它遮住了整个屏幕。所以我认为,问题可能在于
fragment\u watchlist.xml
watchlist fragment.java
,而不是
content\u main.xml
activity\u main.xml
。感谢您的回复。我刚刚尝试添加了
android:layout_marginTop=“?attr/actionBarSize”
,发现它只是将片段视图向下移动了一点。工具栏仍然不可见。嘿,Suraj,我已经为问题添加了所有必要的文件。Bdw,当我尝试使用其他
片段
时,它只有“Hello World”文本,没有任何花哨的内容,工作正常。只有使用
fragment\u watchlist.xml
这种奇怪的事情才会发生。它遮住了整个屏幕。所以我认为,问题可能在于
fragment\u watchlist.xml
watchlist fragment.java
而不是
content\u main.xml
activity\u main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/app_name"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    app:srcCompat="@mipmap/ic_launcher_round" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:text="@string/app_name"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

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

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_watchlist"
        android:icon="@drawable/ic_menu_camera"
        android:title="Watchlist" />

    <item
        android:id="@+id/nav_profile"
        android:icon="@drawable/ic_menu_manage"
        android:title="Profile" />
    <item
        android:id="@+id/nav_suggestions_or_complaints"
        android:icon="@drawable/ic_menu_manage"
        android:title="Suggestions / Complaints" />
    <item
        android:id="@+id/nav_upcoming_features"
        android:icon="@drawable/ic_menu_manage"
        android:title="Upcoming Features" />
</group>
</menu>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never" />
</menu>
 <FrameLayout
        android:visibility="visible"
        android:layout_marginTop="?attr/actionBarSize"
        android:id="@+id/flContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </FrameLayout>