Android Recipes笔记

本文详细介绍了如何使用AndroidRecipes书籍进行Android开发学习,包括使用setContentView方法的不同方式及其背后的原理,以及如何正确使用Android主题来定制应用外观。同时,提供了常见主题的对比与建议,帮助开发者在实际项目中更好地应用这些知识。

最近发现了一本好书,感觉这本书比较适合已经对android比较熟悉的但是还不够系统的人去学习。

书名:android recipes

也准备写点博客去记录读书的一些笔记。

在我们新建一个Activity的时候都知道会重写一个onCreate方法,示例如下

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_main);
    }

我们知道setContentView是为了将布局文件给用户看,在这里setContentView还可以这样写:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        RelativeLayout layout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_main,null);
        setContentView(layout);
    }

这样作用和上面代码完全相同,我们通过getLayoutInflater().inflate方法得到了一个view,再直接将这个view set进去,
而对于inflate()方法,查看代码:

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
        return inflate(resource, root, root != null);
    }

最完整的有三个参数

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)

resource 是你想要return的那个view的id 既R.layout.activity_main;

第二个参数是指这个view所在的父布局,

第三个参数为是否添加到父布局。

因为我们此处直接将view set到activity,因此第二个传null,第三个不传,执行root!=null,默认传值为false。

下面为原书解释:

The second parameter to inflate() is the parent ViewGroup, and this is extremely important
because it defines how the LayoutParams from the inflated layout are interpreted. Whenever
possible, if you know the parent of this inflated hierarchy, it should be passed here; 
otherwise, the LayoutParams from the root view of the XML will be ignored. When passing a
parent, also note that the third parameter of inflate() controls whether the inflated layout
is automatically attached to the parent. We will see in future recipes how this can be useful
for doing custom views. In this instance, however, we are inflating the top-level view of our
activity, so we pass null here.

另外我们在这里看一下常用的主题,经常傻傻分不清楚,这本书里也给出了建议:

1. Theme.Light:这个主题的推荐背景色和用户所看的控件颜色有很大差异,在3.0之前的android版本默认使用的主题。

2.Theme.NoTitleBar.Fullscreen: 显而易见,让应用或者activity全屏。

3.Theme.Dialog: 让你的activity以对话框形式出现。

4:Theme.Holo.Light: (API 11支持), 默认有一个actionbar,android 3.0默认使用的主题。

5.Theme.Holo.Light.DarkActionBar:(API 14支持)actionbar为黑色,android 4.0默认使用的主题。

6. Theme.Material.Light:(API 21支持)由名字就可以看出,主题符合android质感设计的思想,支持colorPrimary这种定义,

android 5.0默认使用的主题。

Note:When using the AppCompat Library, other versions for each of these themes should be usedinstead (for example, Theme.AppCompat.Light.DarkActionBar).

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值