Android之Layout总结

之前自己在用Layout的时候用的最多的就是LinearLayout和RelativeLayout了,对其他的其实也只是一知半解。近来也是接触到了更多的Layout在实际中的用途,也算是有了更多的认识。

我现在接触到的比较多的Layout分为几种:LinearLayout,RelativeLayout,FrameLayout,ConstraintLayout。最后再讲一下自己去自定义layout。

LinearLayout

顾名思义,就是一种线性的布局,也是我们最常用到的布局了,一般可以分为水平布局和垂直布局。LinearLayout中有一个我比较喜欢的东西,就是可以通过定义layout_weight来定义布局中每个子view应占的比例大小。这里就不再多做介绍了,做一个简单的布局,效果图及相应代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#96ec77" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="#7cea55" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#4fec17" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#5aa8e9" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="#3e9dec" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:background="#168bec" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:background="#1063a7" />
    </LinearLayout>

</LinearLayout>

RelativeLayout

从名字也可以看出,这是相对布局。它主要针对的,就是view和view之间的布局关系,以及子view和父view之间的布局关系。大致可以分成以下三种类型:

(1)与父view之间的相对关系,这里用一个图来表示位置关系:

android:layout_centerHrizontal:水平居中

android:layout_centerVertical:垂直居中

android:layout_centerInparent:水平+垂直居中

android:layout_alignParentTop:位于父view的上边缘

android:layout_alignParentBottom 位于父view的下边缘

android:layout_alignParentLeft 位于父view的左边缘

android:layout_alignParentRight 位于父view的右边缘

(2)view与view之

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
腾讯X5内核WebView是一款基于Chromium内核的浏览器内核,相比于原生WebView,具有更好的性能和稳定性。以下是一些优化实践总结: 1. 集成腾讯X5内核 在使用WebView时,可以使用腾讯提供的X5内核替代原生内核。在集成X5内核时,需要在build.gradle文件中添加依赖: ``` dependencies { implementation 'com.tencent.tbs.tbssdk:sdk:43697' } ``` 2. 预加载 使用X5内核时,可以通过预加载提高WebView的响应速度。可以在应用启动时预加载X5内核: ``` // 在Application的onCreate方法中 QbSdk.initX5Environment(context, null) ``` 3. 启用多进程 如果应用中使用了多个WebView,可以启用多进程来提高性能和稳定性。可以在AndroidManifest.xml文件中设置: ``` <application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" android:process=":webview"> ``` 4. 启用硬件加速 在使用X5内核时,可以启用硬件加速来提高渲染速度。可以在布局文件中设置: ``` <com.tencent.smtt.sdk.WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" android:layerType="hardware" /> ``` 5. 使用缓存 使用缓存可以减少网络请求,提高WebView的加载速度。可以在初始化WebView时设置: ``` val webSettings = webView.settings webSettings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK ``` 以上是一些常见的优化实践总结,可以根据具体的场景进行调整和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值