Android之Android:layout_weight详解

本文详细介绍了LinearLayout中的权重属性layout_weight的使用方法。包括如何通过设置不同的权重值来调整控件在屏幕上的分布,以及在嵌套LinearLayout时如何正确设置权重避免布局问题。

1、LinearLayout可以为其包含控件指定填充权值layout_weight。 这样就允许其包含的控件可以填充屏幕上的剩余空间。这也避免了所有控件挤成一堆的情况,而是允许他们放大填充所有空白。剩余的空间会按这些控件指定的权值比例分配屏幕。

2、默认情况下,weight的值是0,表示按照控件的实际大小显示;如果weight设置高于零。

3、剩余空间会按照该控件的weight值占所有控件weight的比例分配给该控件。 比如有两个控件,一个weight为1,另外一个是2. 则剩余空间会把1/(1+2)的部分给控件一,另外2/(1+2)的分配给控件二。也就是权值越大,重要度越大。

4、如果LinearLayout包含子LinearLayout,子LinearLayout之间的权值越大的,重要度则越小。如果有LinearLayout A包含LinearLayout C,D,C的权值为2,D的权值为1,则屏幕的2/3空间分给权值为1的D,1/3分给权值为2的C。在LinearLayout嵌套的情况下,子LinearLayout必须要设置权值,否则默认的情况是未设置权值的子LinearLayout占据整个屏幕。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_weight="2">
        <SurfaceView
            android:id="@+id/surfaceView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:layout_gravity="center">
        <Button android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnTakePicture"
            android:gravity="center"
            android:textSize="20px"
            android:text="拍照"/>
    </LinearLayout>
</LinearLayout>
<!-- 示例:播放/暂停按钮 --> <ImageButton android:id="@+id/btnPlayPause" android:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/ic_play" android:contentDescription="@string/desc_play_button" <!-- 使用字符串资源 --> android:layout_marginHorizontal="16dp" android:background="?attr/selectableItemBackgroundBorderless"/> <!-- 示例:当前歌曲标题 --> <TextView android:id="@+id/currentSongTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/current_song" <!-- 使用字符串资源 --> android:textSize="18sp" android:textStyle="bold" android:gravity="center" android:layout_marginTop="16dp"/> 在哪添加<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- 左侧功能区 --> <LinearLayout android:id="@+id/leftPanel" android:layout_width="0dp" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/purple_200" android:padding="16dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintWidth_percent="0.3"> <ImageView android:id="@+id/albumArt" android:layout_width="150dp" android:layout_height="150dp" android:layout_gravity="center_horizontal" android:src="@drawable/music_note" android:contentDescription="@string/album_art" /> <TextView android:id="@+id/currentSongTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/current_song" android:textSize="18sp" android:textStyle="bold" android:gravity="center" android:layout_marginTop="16dp"/> <TextView android:id="@+id/currentArtist" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/artist" android:textSize="16sp" android:gravity="center"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center" android:layout_marginTop="32dp"> <ImageButton android:id="@+id/btnPrev" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/ic_prev" android:background="?attr/selectableItemBackgroundBorderless" android:contentDescription="@string/previous"/> <ImageButton android:id="@+id/btnPlayPause" android:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/ic_play" android:layout_marginHorizontal="16dp" android:background="?attr/selectableItemBackgroundBorderless" android:contentDescription="@string/play_pause"/> <ImageButton android:id="@+id/btnNext" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/ic_next" android:background="?attr/selectableItemBackgroundBorderless" android:contentDescription="@string/next"/> </LinearLayout> <SeekBar android:id="@+id/songProgress" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="32dp"/> <TextView android:id="@+id/currentTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0:00" android:layout_gravity="start"/> <TextView android:id="@+id/totalTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0:00" android:layout_gravity="end"/> </LinearLayout> <!-- 右侧歌曲列表 --> <androidx.recyclerview.widget.RecyclerView android:id="@+id/songList" android:layout_width="0dp" android:layout_height="match_parent" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" app:layout_constraintStart_toEndOf="@id/leftPanel" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintWidth_percent="0.7"/> </androidx.constraintlayout.widget.ConstraintLayout>
最新发布
11-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值