include标签布局以及自定义标题

在Android的性能优化是,可以使用抽象布局标签(include,ViewStub,merge),去除不必要的嵌套和View节点,减少不必要的inflate以及其他Layout。

include标签常用于将布局中的公共部分提取出来供其他layout公用,以实现布局模块化,这在布局编写方便提供了便利。

示例代码:

activity_main.xml

<Linear xmlns:android="http://schemes.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"/>

<include layout="@layout/foot"/>

</LinearLayout>

foot.xml

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layotu_height="match_parent">

<Button

android:id="!id/buton"

android:layout_width="match_parent"

android:layout_height="40dp"

android:layout_above="@+id/text"

android:text="Button"/>

<Text

android:id="@id/text"

android:layout_width="match_parent"

android:layout_height="40dp"

android:layout_alignParentButtom="true"

android:text="@string/app_name"/>

</RelativeLayout>

<include>标签唯一需要的属性是layout属性,指定的需要包含的布局文件。可以定义android:id和android:layout_*属性来覆盖被引入布局根节点的对应属性值


merge标签可用于两种典型情况

<merge/>标签在UI的结构优化中骑着非常重要的作用,他可以删减多余的层级,优化UI。

</merge>多用于替换FrameLayout或者当一个布局包含另一个是,</merge>标签消除视图层次结构中多余的视图组。例如你的主布局文件是相对布局,引入了一个相对布局的include,这时如果include布局使用的RelativeLayout就没意义了,使用的话反而减慢你的UI表现。这时可以使用<merge/>标签优化。


在使用merge后,视图层被简化了成了两层


<ViewStub>标签

viewstub标签同include标签一样可以用来引入一个外部布局,不同的是,viewstub引入的布局默认不会扩张,即既不会占用显示也不会占用位置,从而在解析layout时节省cpu和内存。viewstub常用来引入那些默认不会显示,只在特殊情况下显示的布局,如进度布局、网络失败显示的刷新布局、信息出错出现的提示布局等
footlayout.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="点击也没反应"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        android:layout_toRightOf="@id/button"
        android:layout_marginLeft="20px"
        android:text="友情提供"/>
</RelativeLayout>
11activity_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"
    tools:context="com.baozilichao.android2lesson_07_inerfacelayout.MainActivity">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:text="点击有反应"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/text"/>
    <!--include引入可以达到重用布局的效果,但是,如果不设置位置信息,比较难看,要想设置include标签布局,必须先设置宽高。
    如果id起冲突了,那么以本layout的id为响应id,include导入的layout的id会被覆盖掉-->
    <!--<include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        layout="@layout/footlayout"/>-->
    <ViewStub
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewStub"
        android:layout="@layout/footlayout"/>
</LinearLayout>
1MainActivity.java

public class MainActivity extends AppCompatActivity {
    private Button button1;
    View view1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        引入的布局文件中的控件一样可以通过id直接获取到
        button= (Button) findViewById(R.id.button);
        button1= (Button) findViewById(R.id.button1);
        tv= (TextView) findViewById(R.id.text);
//         根据id拿到viewStub之后,调用inflate()就会将视图显示出来,并且作为返回值返回
        view1=((ViewStub)findViewById(R.id.viewStub)).inflate();
        view1.setVisibility(View.INVISIBLE);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                tv.setText("红黑联盟");
                if(view1.getVisibility() == View.VISIBLE){
                    view1.setVisibility(View.INVISIBLE);
                }else{
                   view1.setVisibility(View.VISIBLE);
                }
            }
        });

    }
}
在开发时 ,系统为我们提供的标题栏往往不能满足我们的需求,这时候,就需要用到自定义标题栏。

使用自定义标题栏遵循以下步骤:

一、自定义标题栏布局(必须保证跟标签是RelativeLayout)

二、在Activity中使用该布局


三、调整style使该布局和标题栏契合


四、让Activity使用该样式

1

1style.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <!--在这里创建自己的style,用于系统主题的使用-->
    <style name="myTitleBar" parent="android:Theme">
        <!--设置原始标题栏的高度-->
        <item name="android:windowTitleSize">50dp</item>
        <!--设置新的标题栏位完全眼神到对其到原始标标题栏的两边-->
        <item name="android:padding">0dp</item>
    </style>
1title_layout.xml标题栏

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.baozilichao.android2lesson_07_mytitlebar">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:theme="@style/myTitleBar"><!--!!!!!!!!!-->
            <intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
1

public class MainActivity extends Activity {
//    自定义标题栏步骤
//    1.自定义标题栏布局文件
//    2.在对应的Activity当中指定使用自定义的标题栏
//    将自定义标题和主题进行融合,创建一个自己的主题
//    配置清单文件,设置使用自定义主题
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        这行代码必须加载到setContentView之前
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_main);
//        设置当前Activity使用自定义标题栏,指定使用的自定义标题栏为刚才创建的布局文件
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.title_layout);
    }




1111

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值