在横向模式下,我的列表视图出现在屏幕的左侧,而右侧是空白的,这是应该的。当用户单击一个项目时,详细信息会显示在屏幕右侧,但列表会消失。它看起来像是在纵向模式下,但显然是在使用横向 xml 布局域。在查看了几十个示例之后,我仍然无法让列表视图与详细信息一起保留在屏幕上。有人可以指出我正确的方向吗?这是我的代码:
BookDetailsActivity:公共类 BookDetailsActivity 扩展 Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//** Getting the orientation ( Landscape or Portrait ) of the screen
int orientation = getResources().getConfiguration().orientation;
//** Landscape Mode
if(orientation == Configuration.ORIENTATION_LANDSCAPE ){
//setting the landscape layout for this activity
setContentView(R.layout.buy_tbfrag);
//get fragment manager for fragment related operations
FragmentManager fm = getFragmentManager();
//get fragment transaction object, which can add, move or replace a fragment
FragmentTransaction ft = fm.beginTransaction();
//getting the existing detailed fragment object, if it already exists.
//the fragment object is retrieved by its tag name
Fragment prevFrag = fm.findFragmentByTag("book.details");
//System.out.println("prevFrag = " + prevFrag);
//** Remove the existing detailed fragment object if it exists */
if(prevFrag!=null) {
ft.remove(prevFrag);
}
//instantiating the fragment BookDetailsFragment
BookDetailsFragment detailsFragment = new BookDetailsFragment();
//creating a bundle object to pass the data (clicked item's position)
//from this activity to fragment
Bundle b = new Bundle();
//setting the data to the bundle object from the Intent
b.putString("load the bundle objects);
...
//setting the bundle object to the fragment
detailsFragment.setArguments(b);
//adding the fragment to the fragment transaction
ft.add(R.id.details_fragment_container, detailsFragment,"book.details");
//add the fragment transaction to backstack
//ft.addToBackStack(null);
//Executing the transaction
ft.commit();
} else {
...
start Portrait mode code...
}
}
Buy_tbfrg xml代码:
<LinearLayout
android:layout_width="200dip"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="3dip" >
<EditText
android:id="@+id/titleSearch"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:background="@drawable/rectangle_box"
android:hint="@string/hint"
android:padding="5dip"
android:textStyle="italic" />
<ListView
android:id="@android:id/list"
android:layout_width="200dip"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<FrameLayout
android:id="@+id/details_fragment_container"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="3.97" >
</FrameLayout>
</LinearLayout>
BookDetailsFragment:公共类 BookDetailsFragment 扩展 Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.book_details, null);
System.out.println("BookDetailsFragment executed");
//Defines the TextViews in R.layout.book_details
TextView bkTitle = (TextView)view.findViewById(R.id.bookTitle);
...
//Retrieve the bundle object passed from BuyFragTab
Bundle b = getArguments();
//Getting the item's clicked position and setting corresponding details
//First check the bundle to ensure the data has been passed
if (b != null) {
bkTitle.setText ("Title: " + b.getString("Selected_Title"));
...
}
return view;
}