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
- Android Fragment的使用 六 适应屏幕大小
- Android图片适应屏幕大小
- Android 如何适应屏幕大小
- Android图片适应屏幕大小
- 适应屏幕大小的思路
- Android <supports-sceeens>的用法,适应屏幕大小
- android程序自动适应屏幕大小
- 背景图片适应屏幕大小的一些方法
- delphi 适应屏幕大小
- UIWebView适应屏幕大小
- Android基础——使用Fragment适应不同屏幕和分辨率
- Android基础——使用Fragment适应不同屏幕和分辨率
- Android基础之使用Fragment适应不同屏幕和分辨率(分享)
- Android基础之使用Fragment适应不同屏幕和分辨率(分享)
- android适应不同的屏幕
- 【Android开发经验】兼容不同的屏幕大小(推荐,最官方的适应屏幕大小的经验)
- 【Android开发经验】兼容不同的屏幕大小(推荐,最官方的适应屏幕大小的经验)
- 【Android开发经验】兼容不同的屏幕大小(推荐,最官方的适应屏幕大小的经验)
- Hibernate持久化类与主键生成策略
- arguments.callee与递归
- Dangling metacharacter
- linux 学习体验2
- tpshop开源商城,切勿使用?
- Android Fragment的使用 六 适应屏幕大小
- 关于如何处理ubuntu16.04无法连接网络问题
- Beautiful People (zoj 2319)
- poj 3036 Honeycomb Walk 暴力枚举 dp
- java学习之:JDBC(查询)
- 简学jQuery技术
- LL(1)递归下降
- NYOJ 170
- (24)直接插入排序