How do I show a fragment in a card view - Android

*爱你&永不变心* 提交于 2019-12-12 06:02:01

问题


I want to show a fragment in a card view of android . help required . I have made some fragments already , say one of them is Settings_fragment.xml

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardView1"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp"
    card_view:cardUseCompatPadding="true"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="4dp"
    card_view:contentPadding="10dp">

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/frame">

           //HERE I WANT TO SHOW A FRAGMENT

    </FrameLayout>

</android.support.v7.widget.CardView>

回答1:


Inside a CardView Add a FrameLayout. Then in Activity get that framelayout reference and use addView() method to add that fragment.

Example:

YourFragment fragment = new YourFragment();
Framelayout layout = (FrameLayout) findViewById(R.id.frameLayout);
layout.addView(fragment);



回答2:


a cardview is a framelayout. so just add the fragment directly to the cardview and treat is as a framelayout.



来源:https://stackoverflow.com/questions/34018015/how-do-i-show-a-fragment-in-a-card-view-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!