簡體   English   中英

為什么在我調用LinearLayout時我的LinearLayout沒有出現?

[英]Why my doesn't LinearLayout appear on FrameLayout when I call it?

我正在嘗試進行動態片段布局。 我正在使用2個xml文件:一個是main_layout ,另一個是fragment1

我使用了Log.i所以我可以知道我的程序在哪里崩潰,但是它引用了正在運行的代碼,但是在main_layout上沒有出現fragment1。

這是我的代碼:

main_layout.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/testFragment"
        android:visibility="gone" >
</FrameLayout>

fragmenta.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/fragmenta" 
    android:background="#F000F0">

    <TextView
        android:id="@+id/fragmentatext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragmenta 1" />

</LinearLayout>

這是我的Side_Fragment ,它擴展了SherlockFragment

View view;
    private final String TAG = "Side_Fragment";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {


            view = inflater.inflate(R.layout.fragmenta, container, false);
            Log.i(TAG,"fragment installed");
            return view;
    }
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
    }


}

這是主要活動

    private final String TAG = "Main Activity";
    FragmentManager fm = getSupportFragmentManager();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        Log.i(TAG,"OnCreate");

        Button add = (Button) findViewById(R.id.button1);

        Log.i(TAG,"called FragmentManager");
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId())
        {
        case R.id.twitter:
            Log.i(TAG,"Twitter is clicked");
             // Check to see if the Fragment back stack has been populated // If not, create and populate the layout.
            Fragment detailsFragment = (Side_Fragment)fm.findFragmentById(R.id.fragmenta);
            Log.i(TAG,"called FragmentManager");
            if (detailsFragment == null) {
            Log.i(TAG,"pass ifstatment");
            FragmentTransaction ft = fm.beginTransaction(); 
            Log.i(TAG,"called FragmentTransaction");
            Side_Fragment test = new Side_Fragment();
            ft.add(R.id.testFragment,test);
            Log.i(TAG,"called add");
            ft.addToBackStack(null);
            ft.commit();
            Log.i(TAG,"called commit");
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            }
            return true;
        case R.id.facebook:
            Log.i(TAG,"Facebook is clicked");
            return true;
        case R.id.refresh:
            Log.i(TAG,"Refresh is clicked");
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }

    }




    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        final MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.i(TAG,"onPause");
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.i(TAG,"onResume");
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.i(TAG,"onStart");
    }

}

您已經將FrameLayout寬度設置android:layout_width="0dp"小: android:layout_width="0dp" 那將使其看不到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM