[英]Android: Fragment layout issue
對於大屏幕,我有兩個片段,它們應該是上/下或左/右。 問題在於一個碎片占用了(幾乎)所有空間。 對於我的活動,我有兩個main.xml(一個用於肖像,一個用於風景)。 但是它們基本上是相似的(除了方向)。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:id="@+id/frag_cont" >
</LinearLayout>
在onCreate中,將片段添加到該布局中:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
OverviewFragment of = new OverviewFragment();
FragmentTransaction tof = getFragmentManager().beginTransaction();
tof.add(R.id.frag_cont, of);
DetailFragmentInitial df = new DetailFragmentInitial();
tof.add(R.id.frag_cont, df);
tof.commit();
}
就onCreateView()而言,這兩個片段看起來有點相似
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.overview, container, false);
}
最后,這些片段的兩個xml文件如下所示,首先是較小的片段:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1">
<ImageView
android:id="@+id/imageLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
android:layout_centerInParent="true" />
<TextView
android:id="@+id/textB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"
android:layout_above="@id/imageLogo"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
現在貪婪的人占據了空間:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="10000" >
<ListView
android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
我假設當我將兩個layout_weight都設置為1時,兩者都將占用相同的空間,但是即使設置為1000:1,較大的也將占用大約2/3的空間。 減輕重量將使整個屏幕的碎片更大,而另一個屏幕根本看不到。 有任何想法嗎?
編輯:
我遵循下面給出的方法。 現在在空間方面要好得多。 在屏幕上旋轉概覽片段(基本上是列表視圖)為空,其他片段仍然可以。
在OverviewFragment.java中,我在onActivityCreated()中重新填充片段:
public void onSaveInstanceState(Bundle bundle)
{
bundle.putSerializable("list", mList);
super.onSaveInstanceState(bundle);
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
if(savedInstanceState != null)
{
mList = (ArrayList<myObject>) savedInstanceState.getSerializable("list");
}
else
{
mAppList = new ArrayList<myObject>();
new getApps().execute();
}
ListView lv = (ListView) getActivity().findViewById(R.id.ListView01);
lv.setOnItemClickListener(new TableItemSelected());
mItemAdapter = new ItemAdapterOverview(getActivity().getApplicationContext(), mList);
lv.setAdapter(mItemAdapter);
}
旋轉屏幕時,首先調用onSaveInstanceState(),然后兩次調用onActivityCreated()。 第一次調用saveInstanceState不為null,第二次調用為null。 無論如何,在這兩種情況下,我都應該看到我的列表,無論是從頭開始還是保存的列表。 為什么不這樣,為什么該方法被調用兩次?
不能完全確定這是否會引起您的問題(我懷疑是的),但是使用FragmentTransaction.add(..)
不會將 Fragment
添加到容器中,實際上是使用該Fragment替換了容器。 這里的問題是,您要將兩個片段都添加到同一容器中,因此它們將占用相同的空間並引起問題。
更好的方法是將兩個容器視圖添加到您的frag_cont
LinearLayout中(例如FrameLayout
),並frag_one
提供兩個單獨的ID(例如frag_one
和frag_two
)。 然后在您的onCreate(..)中,將其更改為:
FragmentTransaction tof = getFragmentManager().beginTransaction();
tof.add(R.id.frag_one, of);
DetailFragmentInitial df = new DetailFragmentInitial();
tof.add(R.id.frag_two, df);
tof.commit();
這樣一來,ID為frag_one
的布局將被OverviewFragment填充,而ID為frag_one
的布局將被frag_two
替換。 然后,您可以使用main.xml中的視圖來調整片段的大小(如果您認為合適)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.