第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用以編程方式創(chuàng)建的內(nèi)容視圖向活動(dòng)添加片段

如何使用以編程方式創(chuàng)建的內(nèi)容視圖向活動(dòng)添加片段

繁華開滿天機(jī) 2019-07-25 19:32:46
如何使用以編程方式創(chuàng)建的內(nèi)容視圖向活動(dòng)添加片段我想將一個(gè)片段添加到以編程方式實(shí)現(xiàn)其布局的Activity。我查看了Fragment文檔,但沒有很多示例描述我需要的內(nèi)容。這是我嘗試編寫的代碼類型:public class DebugExampleTwo extends Activity {     private ExampleTwoFragment mFragment;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         FrameLayout frame = new FrameLayout(this);         if (savedInstanceState == null) {             mFragment = new ExampleTwoFragment();             FragmentTransaction ft = getFragmentManager().beginTransaction();             ft.add(frame.getId(), mFragment).commit();         }         setContentView(frame);     }}...public class ExampleTwoFragment extends Fragment {     @Override     public View onCreateView(LayoutInflater inflater,                               ViewGroup container,                               Bundle savedInstanceState) {         Button button = new Button(getActivity());         button.setText("Hello There");         return button;     }}此代碼編譯但在開始時(shí)崩潰,可能是因?yàn)槲褾ragmentTransaction.add()的錯(cuò)誤。這樣做的正確方法是什么?
查看完整描述

3 回答

?
繁星點(diǎn)點(diǎn)滴滴

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超3個(gè)贊

這是我在閱讀Tony Wong的評(píng)論后想出

public class DebugExampleTwo extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addFragment(android.R.id.content,
                    new DebugExampleTwoFragment(),
                    DebugExampleTwoFragment.FRAGMENT_TAG);
    }}

...

public abstract class BaseActivity extends Activity {

    protected void addFragment(@IdRes int containerViewId,
                               @NonNull Fragment fragment,
                               @NonNull String fragmentTag) {
        getSupportFragmentManager()
                .beginTransaction()
                .add(containerViewId, fragment, fragmentTag)
                .disallowAddToBackStack()
                .commit();
    }

    protected void replaceFragment(@IdRes int containerViewId,
                                   @NonNull Fragment fragment,
                                   @NonNull String fragmentTag,
                                   @Nullable String backStackStateName) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(containerViewId, fragment, fragmentTag)
                .addToBackStack(backStackStateName)
                .commit();
    }}

...

public class DebugExampleTwoFragment extends Fragment {

    public static final String FRAGMENT_TAG = 
        BuildConfig.APPLICATION_ID + ".DEBUG_EXAMPLE_TWO_FRAGMENT_TAG";

    // ...}

科特林

如果您正在使用Kotlin,請(qǐng)務(wù)必查看Google提供的Kotlin擴(kuò)展程序,或者只編寫您自己的擴(kuò)展程序


查看完整回答
反對(duì) 回復(fù) 2019-07-25
?
ibeautiful

TA貢獻(xiàn)1993條經(jīng)驗(yàn) 獲得超6個(gè)贊

public class Example1 extends FragmentActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
          DemoFragment fragmentDemo = (DemoFragment) 
          getSupportFragmentManager().findFragmentById(R.id.frame_container);
          //above part is to determine which fragment is in your frame_container
          setFragment(fragmentDemo);
                       (OR)
          setFragment(new TestFragment1());
        }

        // This could be moved into an abstract BaseActivity 
        // class for being re-used by several instances
        protected void setFragment(Fragment fragment) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = 
                fragmentManager.beginTransaction();
            fragmentTransaction.replace(android.R.id.content, fragment);
            fragmentTransaction.commit();
        }
    }

要將片段添加到Activity或FramentActivity中,它需要一個(gè)Container。該容器應(yīng)該是一個(gè)“ Framelayout”,可以包含在xml中,否則你可以使用默認(rèn)容器android.R.id.content來刪除或替換Activity中的片段。

main.xml中

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 <!-- Framelayout to display Fragments -->
   <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ImageView
        android:id="@+id/imagenext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_margin="16dp"
        android:src="@drawable/next" /></RelativeLayout>


查看完整回答
反對(duì) 回復(fù) 2019-07-25
  • 3 回答
  • 0 關(guān)注
  • 371 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)