Android Studio 控件的百分比布局

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

 首先是依赖的添加,由于我使用的版本相比参考书中的高,Android studio中已经不支持在compile,

 所以我在模块级build.gradle添加如下的依赖:

implementation 'androidx.percentlayout:percentlayout:1.0.0'
dependencies {
implementation 'androidx.percentlayout:percentlayout:1.0.0'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

 

 

 

 

 

 

 

 

 

后面就是对layout中的 *.xml 文件进行修改

这里的话百分比布局中分 PercentRelativeLayout 和 PercentFrameLayout 两种

我采用的是PercentRelativeLayout,代码如下

<androidx.percentlayout.widget.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/row_one_item_one"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:background="#7700ff00"
android:text="w:70%,h:20%"
android:gravity="center"
app:layout_heightPercent="20%"
app:layout_widthPercent="70%"/>

<TextView
android:id="@+id/row_one_item_two"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_toRightOf="@+id/row_one_item_one"
android:background="#396190"
android:text="w:30%,h:20%"
app:layout_heightPercent="20%"
android:gravity="center"
app:layout_widthPercent="30%"/>


<ImageView
android:id="@+id/row_two_item_one"
android:layout_width="match_parent"
android:layout_height="0dp"
android:src="@drawable/img_1"
android:scaleType="centerCrop"
android:layout_below="@+id/row_one_item_one"
android:background="#d89695"
app:layout_heightPercent="70%"/>

<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="@id/row_two_item_one"
android:background="#770000ff"
android:gravity="center"
android:text="width:100%,height:10%"
app:layout_heightPercent="10%"
app:layout_widthPercent="100%"/>


</androidx.percentlayout.widget.PercentRelativeLayout>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

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

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

 

 

猜你喜欢

转载自www.cnblogs.com/shi-win-snoopy/p/12128267.html