项目目录
1,适配手机480*800 density=240
2,适配平板
1280*800 density=160
package com.example.fragmentdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
layout目录下主布局文件
<pre name="code" class="html"><RelativeLayout 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" >
<fragment
android:id="@+id/title"
android:name="com.example.fragmentdemo.FragTitle"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
layout-sw600dp目录下主布局文件
<pre name="code" class="html"><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" >
<fragment
android:id="@+id/title"
android:name="com.example.fragmentdemo.FragTitle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/content_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" >
<fragment
android:id="@+id/content"
android:name="com.example.fragmentdemo.FragContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
package com.example.fragmentdemo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class NewsContentAct extends Activity {
public static void startAct(Context c,String title){
Intent it = new Intent(c,NewsContentAct.class);
it.putExtra("title", title);
c.startActivity(it);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_content);
TextView content = (TextView)findViewById(R.id.content_txt);
content.setText(getIntent().getStringExtra("title"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.news_content, menu);
return true;
}
}
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".NewsContentAct" >
<TextView
android:id="@+id/content_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
<pre name="code" class="html">package com.example.fragmentdemo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
@SuppressLint("NewApi")
public class FragContent extends Fragment {
TextView content;
public static void startAct(Context c,String content,String title){
Intent intent = new Intent(c,FragContent.class);
intent.putExtra("title", title);
intent.putExtra("content", content);
c.startActivity(intent);
}
public void refresh(String title,String c){
if(content!=null){
content.setText(c);
}
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = LayoutInflater.from(getActivity()).inflate(R.layout.news_content, container, false);
content = (TextView)view.findViewById(R.id.content_txt);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/content_txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end" />
</LinearLayout>
package com.example.fragmentdemo;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
@SuppressLint("NewApi")
public class FragTitle extends Fragment implements OnItemClickListener{
ListView titles;
List<String> data;
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.d("tag", "onCreateView");
View view = LayoutInflater.from(getActivity()).inflate(R.layout.news_title, container, false);
titles = (ListView) view.findViewById(R.id.title_list);
titles.setOnItemClickListener(this);
data = getData();
ListAdapter adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,data);
titles.setAdapter(adapter);
return view;
}
private List<String > getData(){
List<String> result = new ArrayList<String>();
result.add("item1");
result.add("item2");
result.add("item3");
result.add("item4");
result.add("item5");
result.add("item6");
result.add("item7");
return result;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.d("tag", "onViewCreated");
super.onViewCreated(view, savedInstanceState);
}
boolean isPan = false;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
if(getActivity().findViewById(R.id.content)!=null){
isPan = true;
}else{
isPan = false;
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Log.d("tag", "click item:"+arg2);
if(isPan){
FragContent fragment =(FragContent) getFragmentManager().findFragmentById(R.id.content);
fragment.refresh(data.get(arg2), data.get(arg2));
}else{
NewsContentAct.startAct(getActivity(), data.get(arg2));
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/title_list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>