• Android Studio 控件的百分比布局


    • 在安卓开发控件的百分比布局学习中,由于我参考的是第一行代码中的资源,其中的Android studio版本过低,导致的一些错误,花了好长时间来解决它。这里我用的是Android studio 3.5.3

     首先是依赖的添加,由于我使用的版本相比参考书中的高,Android studio中已经不支持在compile,所以我在模块级build.gradle添加如下的依赖:

    1 implementation 'androidx.percentlayout:percentlayout:1.0.0'
    1 dependencies {
    2     implementation 'androidx.percentlayout:percentlayout:1.0.0'
    3     implementation fileTree(dir: 'libs', include: ['*.jar'])
    4     implementation 'androidx.appcompat:appcompat:1.1.0'
    5     implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    6     testImplementation 'junit:junit:4.12'
    7     androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    8     androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    9 }

     后面就是对layout中的 *.xml 文件进行修改,这里的话百分比布局中分 PercentRelativeLayout 和 PercentFrameLayout 两种我采用的是PercentRelativeLayout,代码如下

     1 <androidx.percentlayout.widget.PercentRelativeLayout
     2     xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:app="http://schemas.android.com/apk/res-auto"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent">
     6 
     7     <TextView
     8         android:id="@+id/row_one_item_one"
     9         android:layout_width="0dp"
    10         android:layout_height="0dp"
    11         android:layout_alignParentTop="true"
    12         android:background="#7700ff00"
    13         android:text="w:70%,h:20%"
    14         android:gravity="center"
    15         app:layout_heightPercent="20%"
    16         app:layout_widthPercent="70%"/>
    17 
    18     <TextView
    19         android:id="@+id/row_one_item_two"
    20         android:layout_width="0dp"
    21         android:layout_height="0dp"
    22         android:layout_toRightOf="@+id/row_one_item_one"
    23         android:background="#396190"
    24         android:text="w:30%,h:20%"
    25         app:layout_heightPercent="20%"
    26         android:gravity="center"
    27         app:layout_widthPercent="30%"/>
    28 
    29 
    30     <ImageView
    31         android:id="@+id/row_two_item_one"
    32         android:layout_width="match_parent"
    33         android:layout_height="0dp"
    34         android:src="@drawable/img_1"
    35         android:scaleType="centerCrop"
    36         android:layout_below="@+id/row_one_item_one"
    37         android:background="#d89695"
    38         app:layout_heightPercent="70%"/>
    39 
    40     <TextView
    41         android:layout_width="0dp"
    42         android:layout_height="0dp"
    43         android:layout_below="@id/row_two_item_one"
    44         android:background="#770000ff"
    45         android:gravity="center"
    46         android:text="100%,height:10%"
    47         app:layout_heightPercent="10%"
    48         app:layout_widthPercent="100%"/>
    49 
    50 
    51 </androidx.percentlayout.widget.PercentRelativeLayout>

     

    这里我参照了一位博友的博客

    http://blog.csdn.net/lmj623565791/article/details/46695347

    想了解其中的源码分析可以参考这个博客。

     

  • 相关阅读:
    RabbitMQ(四)
    RabbitMQ(三)
    RabbitMQ(二)
    定位
    响应式布局
    学习前端的一些心得
    css样式大全
    常用标签
    HTML
    app 被拒绝原因
  • 原文地址:https://www.cnblogs.com/shi-win-snoopy/p/12128267.html
Copyright © 2020-2023  润新知