【问题标题】:Fragment is occupying full screen in activity片段在活动中占据全屏
【发布时间】:2019-03-02 21:02:15
【问题描述】:

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

预期的行为如下图所示。

我正在使用带有 RecyclerView 的数据绑定。 StockListItem 是用于访问 RecyclerView 中数据的 POJO 类,stock_list_item_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();

    }
}
public class WatchlistFragment extends Fragment {

    private FragmentWatchlistBinding watchlistBinding;
    private RecyclerViewAdapter adapter;
    private List<StockListItem> lstStockItems = new ArrayList<>();

    public WatchlistFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_watchlist, container, 
    false);
        Toast.makeText(getActivity(), "Reached WatchlistFragment.", 
    Toast.LENGTH_SHORT).show();
        watchlistBinding = 
    DataBindingUtil.setContentView(getActivity(),R.layout.fragment_watchlist);
        watchlistBinding.rvMyRecyclerView.setLayoutManager(new 
    LinearLayoutManager(getActivity()));
        watchlistBinding.rvMyRecyclerView.setHasFixedSize(false);

        lstStockItems.add(new StockListItem("Vedanta 
    Ltd","174.45","+5.35","+3.45"));
        lstStockItems.add(new StockListItem("Eicher Motors 
    Ltd","19974.45","+545.35","+13.45"));
        lstStockItems.add(new StockListItem("Adani Green Energy 
    Ltd","34.45","+5.35","+3.45"));
        lstStockItems.add(new StockListItem("Federal Bank 
    Ltd","84.45","+5.35","+3.45"));
        lstStockItems.add(new StockListItem("Hindustan Zinc 
    Ltd","274.45","+5.35","+3.45"));
        lstStockItems.add(new StockListItem("Indian Oil Corporation 
    Ltd","154.45","+5.35","+3.45"));
        lstStockItems.add(new StockListItem("Hindustan Petroleum Corporation 
    Ltd","154.45","+5.35","+3.45"));
        lstStockItems.add(new StockListItem("ITC 
    Ltd","274.45","+5.35","+3.45"));
        adapter = new RecyclerViewAdapter(getActivity(),lstStockItems);
        watchlistBinding.rvMyRecyclerView.setAdapter(adapter);
        return v;
    }}

这就是我的 fragment_watchlist.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>

public class RecyclerViewAdapter extends 
    RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

    private Context ctx;
    private List<StockListItem> lstUsers = new ArrayList<>();
    public RecyclerViewAdapter(Context ctx, List<StockListItem> lstUsers) {
        this.ctx = ctx;
        this.lstUsers = lstUsers;
    }

    @NonNull
    @Override
    public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(@NonNull 
    ViewGroup viewGroup, int i) {

        StockListItemLayoutBinding stockListItemLayoutBinding = 
    DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), 
    R.layout.stock_list_item_layout,viewGroup,false);

        MyViewHolder myViewHolder = new 
    MyViewHolder(stockListItemLayoutBinding);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewAdapter.MyViewHolder 
    viewHolder, int i) {
        StockListItem stockListItem = lstUsers.get(i);
        viewHolder.stockListItemLayoutBinding.setStock(stockListItem);
    }

    @Override
    public int getItemCount() {
        return lstUsers.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder
    {
        StockListItemLayoutBinding stockListItemLayoutBinding;
        public MyViewHolder(@NonNull StockListItemLayoutBinding 
    stockListItemLayoutBinding) {
            super(stockListItemLayoutBinding.getRoot());
            this.stockListItemLayoutBinding = stockListItemLayoutBinding;

        }
    }
}

public class StockListItem
{
    private String StockName;
    private String StockPrice;
    private String StockPriceChange;
    private String StockPriceChangePercent;


    public String getStockPriceChange() {
        return StockPriceChange;
    }

    public String getStockPriceChangePercent() {
        return StockPriceChangePercent;
    }

    public void setStockPriceChange(String stockPriceChange) {
        StockPriceChange = stockPriceChange;
    }

    public void setStockPriceChangePercent(String stockPriceChangePercent) {
        StockPriceChangePercent = stockPriceChangePercent;
    }

    public void setStockName(String stockName) {
        StockName = stockName;
    }

    public void setStockPrice(String stockPrice) {
        StockPrice = stockPrice;
    }

    public String getStockName() {
        return StockName;
    }

    public String getStockPrice() {
        return StockPrice;
    }

    public StockListItem(String stockName, String stockPrice, String 
    stockPriceChange, String stockPriceChangePercent) {
        this.StockName = stockName;
        this.StockPrice = stockPrice;
        this.StockPriceChange = stockPriceChange;
        this.StockPriceChangePercent = stockPriceChangePercent;
    }
}

这就是 stock_list_item_layout.xml 的样子。

<?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>

这就是我的 activity_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">
    </FrameLayout>

</LinearLayout>

app_bar_main.xml 布局文件看起来像-

<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>

content_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>

这就是我的nav_header_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>

这就是我的activity_main_drawer.xm 文件的样子-

<?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>

这就是我的main.xml 文件的样子 -

<?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>

【问题讨论】:

  • 你在哪里添加了工具栏?
  • 导航抽屉代码在哪里
  • 嘿@TejasPandya,你的意思是MainActivity.javaNavigationDraweronNavigationItemSelected方法的视图标记吗?
  • @AbhijeetKharatmol xml 中的工具栏在哪里?
  • 在 xml 中查看导航抽屉的标记

标签: android android-layout android-fragments


【解决方案1】:

只需在您的activity_main 的FrameLayout 中添加android:layout_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>

【讨论】:

  • 感谢您的回复。我刚刚尝试添加android:layout_marginTop="?attr/actionBarSize",发现它只是将片段视图向下移动了一点。工具栏仍然不可见。
  • 嘿 Suraj,我已将所有必要的文件添加到问题中。
  • Bdw,当我尝试使用其他一些 Fragment 时,它只有“Hello World”文本,没有什么花哨的,它工作正常。只有fragment_watchlist.xml 才会发生这种奇怪的事情。它覆盖了整个屏幕。所以我认为,问题可能出在fragment_watchlist.xmlWatchlistFragment.java 而不是content_main.xmlactivity_main.xml
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多