2016-12-11 24 views
0

我是碎片的新成員,在正常工作時遇到一些麻煩。用我現在的代碼,我的ListView與我的工具欄重疊。當我將工具欄移出框架佈局以避免這種情況時,列表視圖停止顯示。我的XML:xml和碎片基礎知識

這裏是我的MainActivity

<LinearLayout 
    android:orientation="horizontal" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.oenders1.oenders1project3.MainActivity"> 

    <android.support.v7.widget.Toolbar 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?attr/colorPrimary" 
     android:theme="?attr/actionBarTheme" 
     android:minHeight="?attr/actionBarSize" 
     android:id="@+id/tbrMain"/> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/fragment_container" 
     xmlns:android="http://schemas.android.com/apk/res/android"/> 

</LinearLayout> 

而且MainFragment

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.oenders1.oenders1project3.MainActivity"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:text="@string/empty" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/txtEmpty" 
      android:layout_weight="1"/> 

     <ListView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/lstToDo" 
      android:layout_weight="1"/> 

    </LinearLayout> 

</LinearLayout> 

和Java:

MainFragment mainFragment = new MainFragment(); 

mainFragment.setArguments(getIntent().getExtras()); 
getSupportFragmentManager() 
      .beginTransaction() 
      .add(R.id.fragment_container, mainFragment) 
      .commit(); 

抱歉,如果這已經被問。我在搜索一個類似的問題時提出了一些問題。感謝您提前提供任何幫助!

+2

你缺少在主XML片段中關閉一個標籤。不知道這是因爲你忘了將它複製到SO還是真的不知道。 –

+0

謝謝,是的,只是錯過了它的副本。 – ownificate

+0

在您的'MainActivity' xml文件中將android:orientation =「horizo​​ntal」更改爲'LinearLayout'的android:orientation =「vertical」 – Shashanth

回答

0

在MainActivity的onCreate方法,添加此

Toolbar toolbar = (Toolbar) findViewById(R.id.tbrMain); 
setSupportActionBar(toolbar); 

你MainActivity應該類似於此

public class MainActivity extends AppCompatActivity { 

    android.support.v7.widget.Toolbar toolbar; 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     toolbar = (Toolbar) findViewById(R.id.tbrMain); 
     setSupportActionBar(toolbar); 

     MainFragment mainFragment = new MainFragment(); 

     mainFragment.setArguments(getIntent().getExtras()); 
     getSupportFragmentManager() 
      .beginTransaction() 
      .add(R.id.fragment_container, mainFragment) 
      .commit(); 
    } 
} 

如果已經添加,發佈您MainActivity類別

+0

非常感謝!我得到它的工作!我想我只是在錯誤的方向上進行了線性佈局! – ownificate