Android Fragment的使用 六 适应屏幕大小

来源:互联网 发布:网络直播电视怎么安装 编辑:程序博客网 时间:2024/05/22 07:05

我这一次使用DialogFragment做一个自动适应屏幕大小的例子,就是当屏幕不是平板那么大时,通过对话框显示Fragment,当屏幕是平板的时候,插入当前的activity显示。
这个实现主要通过创建几个对应不同大小的bools.xml文件,然后使用getResources().getBoolean(R.bool.large_layout) 来实现判断当前屏幕的大小。
使用例子如下:

public class MainActivity extends Activity{   @Override   protected void onCreate(Bundle savedInstanceState)   {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);   }   public void showLoginDialog(View view)   {      FragmentManager fragmentManager = getFragmentManager();      EditNameDialogFragment newFragment = new EditNameDialogFragment();      boolean mIsLargeLayout = getResources().getBoolean(R.bool.large_layout) ;      Log.e("TAG", mIsLargeLayout + "");      if (mIsLargeLayout )      {         // The device is using a large layout, so show the fragment as a         // dialog         newFragment.show(fragmentManager, "dialog");      } else      {         // The device is smaller, so show the fragment fullscreen         FragmentTransaction transaction = fragmentManager               .beginTransaction();         // For a little polish, specify a transition animation         transaction               .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);         // To make it fullscreen, use the 'content' root view as the         // container         // for the fragment, which is always the root view for the activity         transaction.replace(R.id.id_ly, newFragment)               .commit();      }   }}public class EditNameDialogFragment extends DialogFragment{   @Override   public View onCreateView(LayoutInflater inflater, ViewGroup container,         Bundle savedInstanceState)   {      if (getResources().getBoolean(R.bool.large_layout))      {         getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);      }      View view = inflater.inflate(R.layout.fragment_edit_name, container,            false);      return view;   }}
activity_main<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="vertical" ><Button    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:onClick="showLoginDialog"    android:text="自动适应屏幕" />    <FrameLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/id_ly"        ></FrameLayout></LinearLayout>fragment_edit_name<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content" >    <TextView        android:id="@+id/id_label_your_name"        android:layout_width="wrap_content"        android:layout_height="32dp"        android:gravity="center_vertical"        android:text="Your name:" />    <EditText        android:id="@+id/id_txt_your_name"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_toRightOf="@id/id_label_your_name"        android:imeOptions="actionDone"        android:inputType="text" />    <Button        android:id="@+id/id_sure_edit_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_below="@id/id_txt_your_name"        android:text="ok"/>    <Button        android:id="@+id/id_cancel_edit_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/id_txt_your_name"        android:text="cancel"        android:layout_alignParentLeft="true"/></RelativeLayout>

再见

0 0
原创粉丝点击