我正在尝试了解有关 Android UI 的更多信息,并且正在尝试几种不同的 UI 设计。现在我正在使用 Fragments 使用 ViewPager 并尝试模拟主屏幕,但它不适用于我的所有设备。在我的 7 英寸平板电脑上它看起来很好(见下图),但在 10 英寸平板电脑上,页面之间的过渡似乎拖了。它不仅速度慢,而且图像在屏幕上永久倾斜(见下图)。两款平板电脑都运行 Android 4.0.3。有谁知道问题是什么以及如何解决?
图片:
7" 平板电脑工作正常:
尝试向左滑动一页后的 10 英寸平板电脑:
代码:
MainActivity.java
package com.example.layouttest;
import java.util.ArrayList;
import java.util.List;
import com.example.layouttest.MyFragment.OnFragmentInteractionListener;
import android.support.v4.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity implements OnFragmentInteractionListener{
MyPageAdapter pageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
pager.setCurrentItem(pager.getAdapter().getCount()/2);
}
private List<Fragment> getFragments() {
List<Fragment> fragList = new ArrayList<Fragment>();
fragList.add(MyFragment.newInstance("Fragment 1"));
fragList.add(MyFragment.newInstance("Fragment 2"));
fragList.add(MyFragment.newInstance("Fragment 3"));
fragList.add(MyFragment.newInstance("Fragment 4"));
fragList.add(MyFragment.newInstance("Fragment 5"));
return fragList;
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
activity_main.xml
<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.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
MyPageAdapter.java
package com.example.layouttest;
import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm) {
super(fm);
fragments = new ArrayList<Fragment>();
}
public MyPageAdapter(FragmentManager fm, List<Fragment> list){
super(fm);
fragments = list;
}
@Override
public Fragment getItem(int arg0) {
return fragments.get(arg0);
}
@Override
public int getCount() {
return fragments.size();
}
public void addItem(Fragment f){
fragments.add(f);
}
public void removeItem(Fragment f){
fragments.remove(f);
}
public void removeItem(int i){
fragments.remove(i);
}
}
MyFragment.java
package com.example.layouttest;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {@link android.support.v4.app.Fragment} subclass. Activities that
* contain this fragment must implement the
* {@link MyFragment.OnFragmentInteractionListener} interface to handle
* interaction events. Use the {@link MyFragment#newInstance} factory method to
* create an instance of this fragment.
*
*/
public class MyFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
// TODO: Rename and change types of parameters
private String text;
private OnFragmentInteractionListener mListener;
/**
* Use this factory method to create a new instance of this fragment using
* the provided parameters.
*
* @param param1
* Parameter 1.
* @param param2
* Parameter 2.
* @return A new instance of fragment MyFragment.
*/
// TODO: Rename and change types and number of parameters
public static MyFragment newInstance(String param1) {
MyFragment fragment = new MyFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}
public MyFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
text = getArguments().getString(ARG_PARAM1);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_my, container, false);
TextView message = (TextView)v.findViewById(R.id.textView);
message.setText(text);
return v;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated to
* the activity and potentially other fragments contained in that activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
}
fragment_my.xml
<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"
tools:context=".MyFragment" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_blank_fragment"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Button" />
</RelativeLayout>