本文我们将对上一篇文章中的内容进行调整,使其能够做到兼顾屏幕分辨率。不废话,直接上代码吧。
创建资源文件 res/values-large/refs.xml
<resources>
<item type="layout" name="activity_book_list">
@layout/activity_book_twopane
</item>
</resources>
布局文件 activity_book_list.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:name="com.toby.personal.testlistview.BookListFragment"
android:id="@+id/book_list"
/>
布局文件 activity_book_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/book_detail_container"
/>
主程序文件 BookListActivity 的代码:
package com.toby.personal.testlistview;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
/**
* Created by toby on 17-3-30.
*/
public class BookListActivity extends Activity implements BookListFragment.Callbacks {
private boolean mTwoPane;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book_list);
if (findViewById(R.id.book_detail_container) != null) {
mTwoPane = true;
((BookListFragment) getFragmentManager().findFragmentById(R.id.book_list))
.setActivateOnItemClick(true);
}
}
@Override
public void onItemSelected(Integer id) {
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putInt(BookDetailFragment.ITEM_ID, id);
BookDetailFragment fragment = new BookDetailFragment();
fragment.setArguments(arguments);
getFragmentManager().beginTransaction().replace(R.id.book_detail_container, fragment).commit();
} else {
Intent detailIntent = new Intent(this, BookDetailActivity.class);
detailIntent.putExtra(BookDetailFragment.ITEM_ID, id);
startActivity(detailIntent);
}
}
}
对应的 BookDetailActivity 的代码:
package com.toby.personal.testlistview;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.MenuItem;
/**
* Created by toby on 17-3-30.
*/
public class BookDetailActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book_detail);
ActionBar actionBar = getActionBar();
if (null != actionBar) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
if (savedInstanceState == null) {
BookDetailFragment fragment = new BookDetailFragment();
Bundle arguments = new Bundle();
arguments.putInt(BookDetailFragment.ITEM_ID,
getIntent().getIntExtra(BookDetailFragment.ITEM_ID, 0));
fragment.setArguments(arguments);
getFragmentManager().beginTransaction().add(R.id.book_detail_container, fragment).commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Intent intent = new Intent(this, BookListActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
该示例在手机的虚拟机上的运行效果:
该示例在平板的虚拟机上的运行效果:
参考文献:《疯狂Android讲义(第2版)》