Tricks of Android's GUI

*/-->

Tricks of Android's GUI

1 layoutweight

In LinearLayout, the default weight for all views is 0, so if you specify any weight value greater than 0 to only one view, then that view fills whatever space remains after all views are given the space they require. It can be used in screen adaptation.

2 Saving Data Across Rotation

2.1 Problem

We should understand that activity would be destroyed and recreated when the rotation happen. Obviously, so are the data. So you have to do something before you rotate the devices. Also android will never destroy a running activity to reclaim memory, only in the paused or stoped state. So be careful if your activity turn into these states. And remember an activity's onSaveInstanceState(..) method would be called if it's paused or stopped.

2.2 Solu

We can ride onSaveInstanceState(…) to save addtional data to the bundle and then read that data back in onCreate().
#+BEGIN Java
@Over
pc void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
uper.onSaveInstanceState(outState, outPersistentState);
outState.putInt(KEYINDEX, mCurrentIndex);
}
#+ENDSRC
In onCreate(), you can

if(saveInstanceState != null){
//your code
}

3 ActivityManager

4 Fragment

Fragment has to rely on Activity, a fragment is a controller object that can manager a user interface which is a entire screen or a part of the screen.

4.1 Two Approach to Hosting Fragment

One approach is to add fragment into activity's layout, but it's not inflexible. Because you can't swap out the fragment during the activity lifecycle.
Another approach is managered in activity classs. Although it's complicate, but it is flexible and we can handle fragment.

4.2 FragmentManager

Similar with Activity, there is FragmentManager to manager the Fragment. But diffierent with ActivityManager, FragmentManager must be explicit called to handle fragment.

5 dp, px,sp

In MDPI: dp = px = sp
In HDPI: px < dp = sp
In HDPI with larger text: px < dp < sp
Recommendtion: use dp and sp, not px or else.

6 Layout Parameters

Attributes whose names do not begin with layout_ are directions to the widget. When it is inflated, the widget calls a method to configure itself based on each of these attributes and their values.
When an attribute’s name begins with layout_ , that attribute is a direction to that widget’s parent. These attributes are known as layout parameters, and they tell the parent layout how to arrange the child element within the parent.

7 Passing Data Between Two Fragments

There are two Fragments named A and B, if you want to create B in A. You can use B.newInstance() method, and the method carry the data that you want to passing from A to B. Then if there are data return from B, you can call Fragment.setTargeFragment() (similar with Activity.startActivityForResult()) in A, and call getTargetFragment() in B. Then we have a connection between A and B, but we still have to call onActivityResult() in A.

8 AppCompatActivity

8.1 Action Bar

We should note that the res/menu xml files now need to use an app: prefix for showAsAction as shown below:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_refresh"
android:title="@string/action_refresh"
android:icon="@drawable/ic_action_refresh"
app:showAsAction="ifRoom" />
</menu>

You can find more details on https://github.com/codepath/android_guides/wiki/Extended-ActionBar-Guide

8.2 Ancestral Navigation

You should do as shown below:

((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);

8.2.1 Call setDisplayHomeAsUpEnabled() in Fragment

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(getActivity(), CrimeListActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
getActivity().finish();
default:
return super.onOptionsItemSelected(item);
}
}

8.2.2 Add Parent Activity metadata

<activity>
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".parentActivity"/>
</activity>

In onOptionsItemSelected()

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (NavUtils.getParentActivityName(getActivity()) != null) {
NavUtils.navigateUpFromSameTask(getActivity());
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Before you calling NavUtils,you should make sure the current activity has a parent activity.

onCreateView(){
if(NavUtils.getParentActivityName(getActivity())!=null){
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}

This way is better.

9 ListView Empty

If the list is empty, then the listview will be a big void. User can't interact with that GUI, so we should add a view if the list is empty, and remove it when the list isn't empty.

9.1 Solution 1

Using setEmptyView():

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ImageButton
android:id="@+id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="25dp"
android:background="@android:drawable/ic_input_add"/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/> </FrameLayout>

In onCreateView()

View v = inflater.inflate(R.layout.activity_fragment, container, false);
ListView listview = (ListView) v.findViewById(android.R.id.list);
ImageButton imgBtn = (ImageButton) v.findViewById(R.id.empty);
imgBtn.setVisibility(View.GONE);
listview.setEmptyView(imgBtn);
// View v = inflater.inflate(R.layout.activity_fragment, null, false);
imgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Crime crime = new Crime();
CrimeLab.get(getActivity()).addCrime(crime);
Intent intent = new Intent(getActivity(), CrimePagerActivity.class);
intent.putExtra(CrimeFragment.EXTRA_CRIME, crime.getCrimeId());
startActivityForResult(intent, 0);
getActivity().findViewById(R.id.empty).setVisibility(View.INVISIBLE);
}
});

9.2 TODO Solution2

I haven't figured out yet.

Author: mlhy

Created: 2015-10-28 Wed 12:49

Emacs 24.5.1 (Org mode 8.2.10)

Tricks of Android's GUI的更多相关文章

  1. 图解Android - Android GUI 系统 (1) - 概论

    Android的GUI系统是Android最重要也最复杂的系统之一.它包括以下部分: 窗口和图形系统 - Window and View Manager System. 显示合成系统 - Surfac ...

  2. 国庆大礼包:2014年最全的ANDROID GUI模板和线框图免费下载

    距离上次分享GUI模板有很长时间了,这段时间里设计趋势不断变化,谷歌推出了最新的Android L以及全新的界面设计,UI设计师又有得忙了,今天收集了一组实用的GUI模板和线框图,包含最新的Andro ...

  3. Android Weekly Notes Issue #221

    Android Weekly Issue #221 September 4th, 2016 Android Weekly Issue #221 ARTICLES & TUTORIALS And ...

  4. 浅析 Android 的窗口

    来源:http://bugly.qq.com/bbs/forum.php?mod=viewthread&tid=555&fromuid=6   一.窗口的概念 在开发过程中,我们经常会 ...

  5. Android 开发之 ---- 底层驱动开发(一) 【转】

    转自:http://blog.csdn.net/jmq_0000/article/details/7372783 版权声明:本文为博主原创文章,未经博主允许不得转载. 驱动概述 说到 Android ...

  6. 游戏引擎/GUI的设计与实现-序

    几年前写<嵌入式GUI FTK设计与实现>,没写几篇就停止更新了.当时自己研究过MicroWindows, X Window, DirectFB, GTK+和Android的GUI,又写过 ...

  7. Android 开发之 ---- 底层驱动开发(一)

    驱动概述 说到 android 驱动是离不开 Linux 驱动的.Android 内核采用的是 Linux2.6 内核 (最近Linux 3.3 已经包含了一些 Android 代码).但 Andro ...

  8. 浅析Android的窗口

    一.窗口的概念 在开发过程中,我们经常会遇到,各种跟窗口相关的类,或者方法.但是,在 Android 的框架设计中,到底什么是窗口?窗口跟 Android Framework 中的 Window 类又 ...

  9. (转) Android的Window类

    Android的Window类 2011-03-25 10:02 by Keis, 110 visits, 网摘, 收藏, 编辑 Android的Window类(一)  Android的GUI层并不复 ...

随机推荐

  1. linux 替换jdk指定jar包

    我的bug是:jdk1.8的安全策略和腾讯邮箱服务有冲突.我知道的解决方法: 1更换低版本安全策略相关的jar包.(windows:http://www.cnblogs.com/dennyzhangd ...

  2. == 与 equals区别(HashCode方法)

    1:==分析 1.2:基本类型比较 判断基本类型的数值是不是相等 1.3:对象类型比较 判断两个引用是不是指向同一个对象,即内存地址是不是相等. 2:equals分析 来判断对象内容是不是相等,一般有 ...

  3. 吴裕雄--天生自然MySQL学习笔记:MySQL 删除数据库

    使用普通用户登陆 MySQL 服务器,可能需要特定的权限来创建或者删除 MySQL 数据库,所以使用 root 用户登录,root 用户拥有最高权限. 在删除数据库过程中,务必要十分谨慎,因为在执行删 ...

  4. 正文内容 python3编码问题

    来源:http://www.jb51.net/article/92006.htm 以下是全文: 这两天写了个监测网页的爬虫,作用是跟踪一个网页的变化,但运行了一晚出现了一个问题....希望大家不吝赐教 ...

  5. update-help : 无法更新带有 UI 区域性 {zh-CN} 的模块“WindowsUpdateProvider”帮助: 在 HelpInfo XML 文件中检索不到 UI 区域性 zh-CN

    环境 OS: Windows10 企业版 LTSC x64 CPU: Intel i5-7500 CPU 3.4GHz PowerShell:5.1.17763.503 描述 更新powershell ...

  6. 18 11 05 继续补齐对python中的class不熟悉的地方 和 pygame 精灵

    ---恢复内容开始--- class game : #历史最高分----- 是定义类的属性 top_score =0 def __init__(self, player_name) : #是定义的实例 ...

  7. 对Java面向对象中多态的理解

    理解的要点:多态意味着父亲的变量可以指向子类对象 面向对象程序设计的三大支柱是封装.继承和多态 封装对外把相应的属性和方法实现的细节进行了隐藏.继承关系使一个子类继承父亲的特征,并且加上了一些新的特征 ...

  8. 微信小程序官方示例 官方weui-wxss下载于安装 详解

    1.小程序示例源码:https://github.com/wechat-miniprogram/miniprogram-demo 2.微信 weui下载地址:https://github.com/we ...

  9. autorclone使用心得

    一边使用一边更新. 0x00  SAs最坑的那地方在于,当我新建了一个group,却只能每天添加100个SAs.但是autorclone在本地调用的SAs却有500个,这样每次copy的时候,auto ...

  10. AES学习小结

    AES是基于数据块的加密方式,即每次处理的数据是一块(16字节),当数据不是16字节的倍数时填充,这就是所谓的分组密码(区别于基于比特位的流密码),16字节是分组长度. AES支持五种模式:CBC,C ...