當我在手機模擬器上運行我的應用程序時,我看到我的列表片段出現不希望的外觀。在平板電腦模擬器上它會出現一次,但在電話模擬器上會出現兩次。我該如何解決這個錯誤,讓列表只出現在手機模擬器上一次?ListFragment在手機上出現兩次
MainActivity.java
public class MainActivity extends ActionBarActivity {
private boolean mTwoPane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentMainList newFragment = new FragmentMainList();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.master_container, newFragment);
transaction.commit();
if (findViewById(R.id.detail_container) != null) {
mTwoPane = true;
}
}
}
activity_main.xml中
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/master_container"
android:name="com.apptacularapps.exitsexpertlondonlite.FragmentMainList"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentMainList"
tools:layout="@android:layout/list_content"/>
activity_main.xml中(sw600dp)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/master_container"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:id="@+id/detail_container"/>
</LinearLayout>
個FragmentMainList.java
public class FragmentMainList extends android.support.v4.app.Fragment {
ListView list_main;
String[] listContent = {
"Item 1",
"Item 2",
"Item 3"
};
private boolean mTwoPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_main_list, container, false);
list_main = (ListView)v.findViewById(R.id.list_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listContent);
list_main.setAdapter(adapter);
return v;
}
}
你能從佈局文件夾發佈activity_main.xml嗎?不是sw600dp – km86
他們都在那裏。這些是我在佈局文件夾中唯一的兩個activity_main.xml文件。 ** activity_main.xml **來自我的佈局文件夾。 – MacaronLover
將其從片段更改爲RelativeLayout並查看會發生什麼。 – km86