【发布时间】:2015-08-18 07:43:59
【问题描述】:
当我在手机模拟器上运行我的应用程序时,我看到我的列表片段出现了不受欢迎的外观。在平板电脑模拟器上出现一次,但在手机模拟器上出现两次。如何解决此错误以使列表仅在手机模拟器上出现一次?
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
-
他们都在那里。这是我在布局文件夹中仅有的两个 activity_main.xml 文件。 activity_main.xml 来自我的布局文件夹。
-
把它从fragment改成RelativeLayout,看看会发生什么。
-
好吧,它工作得很好,但老实说,虽然我不得不使用
标签 -
但是你呢?您正在为平板电脑 UI 使用 RelativeLayout。如果没有,您可以使用类似的代码 if (findViewById(R.id.detail_container) != null) { FragmentMainList newFragment = new FragmentMainList(); FragmentTransaction 事务 = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.master_container, newFragment); transaction.commit();mTwoPane = true; }
标签: java android xml android-listview android-listfragment