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. 对PHP-GC(垃圾回收)的一点理解

    一直对php的垃圾回收机制不明不白故自己开贴记录下. 首先,说到垃圾回收,得先知道什么情况下才能产生垃圾. 如果一个变量refcount在增加,说明在被使用,不是垃圾. 如果一个变量的refcount ...

  2. CPU构架和工作原理

    -- CPU -- -- CPU 由三部分组成:时钟:控制单元:算术逻辑单元 -- -- -- 时钟:对CPU内部操作与系统其他硬件进行同步: -- -- -- 控制单元:控制机器指令的执行顺序: - ...

  3. MJJ玩磁铁

    题目: Problem D: MJJ玩磁铁 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 139  Solved: 9[Submit][Status][ ...

  4. uWSGI调整buffer-size

    https://uwsgi-docs.readthedocs.io/en/latest/Options.html#buffer-size buffer-size argument: required_ ...

  5. 设计模式讲解2:static proxy和decorator的不同点

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 在常见的23种设计模式中,static proxy和decorator在代码结构上是特别相似的.那它们的不 ...

  6. Nginx模块-ngx_http_mirror_module-流量复制【转】

    Nginx流量复制# 需求# 将生产环境的流量拷贝到预上线环境或测试环境,这样做有很多好处,比如: 可以验证功能是否正常,以及服务的性能: 用真实有效的流量请求去验证,又不用造数据,不影响线上正常访问 ...

  7. 课程报名 | 5G时代的视频云服务关键技术与实践

    6月3日,工业和信息化部宣布将于近期发放5G商用牌照.这也意味着,中国正式进入了5G时代. 5G身上有很多新标签:"大规模天线"."新的编码技术"." ...

  8. 【@ConfigurationProperties注解】Not Found The requested URL /spring-boot/docs/2.2.2.RELEASE/reference/html/configuration-metadata.html was not found on this server.

    <!-- 配置文件自动映射 --> <dependency> <groupId>org.springframework.boot</groupId> & ...

  9. 【MySQL优化】数据库结构优化

    原则: 设计表结构,字段类型,最小化磁盘存储的空间,减少IO.数据库操作中最为耗时的操作就是 IO 处理,大部分数据库操作 90% 以上的时间都花在了 IO 读写上面.所以尽可能减少 IO 读写量,可 ...

  10. JS—Function类型

    1.函数的声明方式有三种普通函数的声明方式function box(num1,num2){ return num1+num2;}alert(box(1,2)); 使用变量初始化函数var box = ...