【J】layout_weight

本文详细解释了LinearLayout中layout_属性的作用及区别,特别是layout_weight和weight_sum的使用技巧。阐述了控件宽度分配原则,强调了空间宽度优先于layout_weight的原则,并介绍了如何解决TextView在LinearLayout中的基线对齐问题。

layout_ 相关属性的说明,layout_weight详解,weight _sum特别提示

1、有无layout_开头的属性的区别

layout_开头的布局参数是交给父容器计算的,没有layout_开头的布局参数由空间本身去进行处理
*例如:***layout_gravity和gravity的区别:
layout_gravity是相对于父控件而言的,而gravity是相对于自己本身而言。

2、Linearlayout的布局宽度的分配

Linearlayout的布局宽度优先于layout_weight
尺寸计算,先按照空间申明的尺寸进行分配,然后再将剩余的尺寸分配给wieght
LinearLaout中的layout_weight属性会首先按控件声明的尺寸进行分配,然后降剩下的尺寸按weight分配,控件宽度+父控件剩余宽度*比例
结论:
空间宽度+父控件剩余宽度*比例
剩余尺寸=父布局尺寸-子布局总尺寸
控件宽度+剩余尺寸乘以比例
备注:
layout_weight先按照空间声明分配,然后按weight分配 剩余的可以是负数

layout_weight 某个控件text多行,第一行对齐,baselineAligned = “false” 可使控件对齐
match_parent + layout_weight
问题原因:TextView在LinearLayout中会参考父类的baseline基线
解决方法:android:baselineAligned=”false”

3、特别的【weight_sum】

在父控件中声明属性:weight_sum

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/wallpaper1" android:orientation="vertical" tools:context=".activity.MainActivity"> <Button android:id="@+id/bt_start_float" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" android:text="打开悬浮窗"> </Button> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="15dp" android:layout_weight="0.5" android:gravity="center_vertical" android:orientation="horizontal" android:paddingLeft="12dp" android:paddingTop="6dp" android:paddingRight="12dp" android:paddingBottom="6dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_logo" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="导播系统" android:textColor="@color/text_color" android:textSize="@dimen/text_16sp" android:textStyle="bold" /> <View android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" /> <ImageView android:id="@+id/ic_zhuti" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_zhuti" /> <ImageView android:id="@+id/ic_shumingshuo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="14dp" android:background="@mipmap/ic_shumingshuo" /> <ImageView android:id="@+id/ic_tuichu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="14dp" android:background="@mipmap/ic_tuichu" /> <ImageView android:id="@+id/ic_shezhi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="14dp" android:background="@mipmap/ic_shezhi" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginLeft="12dp" android:layout_marginRight="12dp" android:layout_weight="1" android:orientation="horizontal"> <LinearLayout android:id="@+id/start_living" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@mipmap/bg_ground11" android:gravity="center_vertical" android:orientation="vertical" android:padding="12dp"> </LinearLayout> <LinearLayout android:id="@+id/btn_shortcut" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_weight="1" android:background="@mipmap/bg_ground111" android:gravity="center_vertical" android:orientation="vertical" android:padding="12dp"> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_margin="12dp" android:layout_weight="5.1" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <TextureView android:id="@+id/texture" android:layout_width="match_parent" android:layout_height="match_parent" android:keepScreenOn="true" android:visibility="gone" /> <com.cw.liveguidestation.view.layer.MultiLayerEditor android:id="@+id/layerEditor" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> <LinearLayout android:id="@+id/ll_sound" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginStart="10dp" android:layout_weight="1" android:orientation="vertical" android:visibility="visible"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/nav_rlv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/bg_nav" android:orientation="horizontal" /> <FrameLayout android:id="@+id/fragment_container1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:id="@+id/ll_video" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginStart="10dp" android:layout_weight="1" android:orientation="vertical" android:visibility="gone"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout 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:gravity="center_vertical" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/bg_de" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/jiweiChange" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一号机位" android:textColor="@color/text_color" android:textSize="@dimen/text_12sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@mipmap/ic_xiala1" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:background="@mipmap/btn_ok" android:gravity="center" android:paddingLeft="5dp" android:paddingTop="2dp" android:paddingRight="5dp" android:paddingBottom="2dp" android:text="确认" android:textColor="@color/text_color" android:textSize="@dimen/text_12sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:background="@mipmap/btn_esc" android:gravity="center" android:paddingLeft="5dp" android:paddingTop="2dp" android:paddingRight="5dp" android:paddingBottom="2dp" android:text="停止" android:textColor="@color/text_color" android:textSize="@dimen/text_12sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_bg" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:gravity="center" android:text="背景画面" android:textColor="@color/text_color" android:textSize="@dimen/text_12sp" /> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_h" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:background="@mipmap/ic_v" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <LinearLayout android:id="@+id/llImageBg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="图片" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:id="@+id/ivImageBg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:background="@mipmap/sel_l" android:visibility="invisible" /> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:id="@+id/llCameraBg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="摄像头" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:id="@+id/ivCameraBg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:background="@mipmap/sel_l" android:visibility="invisible" /> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:id="@+id/llVideoBg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="视频" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:id="@+id/ivVideoBg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:background="@mipmap/sel_l" android:visibility="invisible" /> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:id="@+id/llSlideBg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="幻灯片" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:id="@+id/ivSlideBg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:background="@mipmap/sel_l" android:visibility="invisible" /> </LinearLayout> </LinearLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rvContentBg" android:layout_width="match_parent" android:layout_height="wrap_content" /> <!-- <ImageView--> <!-- android:layout_width="wrap_content"--> <!-- android:layout_height="wrap_content"--> <!-- android:layout_marginTop="10dp"--> <!-- android:background="@mipmap/ic_add" />--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_bg" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:gravity="center" android:text="主播画面" android:textColor="@color/text_color" android:textSize="@dimen/text_12sp" /> <androidx.appcompat.widget.SwitchCompat android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" app:thumbTint="@color/text_color" app:trackTint="@color/switch_color" /> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_h" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:background="@mipmap/ic_v" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="摄像头" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:background="@mipmap/sel_l" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="数字人" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:background="@mipmap/sel_l" /> </LinearLayout> </LinearLayout> <!-- <androidx.recyclerview.widget.RecyclerView--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="wrap_content"/>--> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:background="@mipmap/ic_add" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="扣绿幕" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <androidx.appcompat.widget.SwitchCompat android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" app:thumbTint="@color/text_color" app:trackTint="@color/switch_color" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="色相值" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <androidx.appcompat.widget.AppCompatSeekBar style="@style/CustomVerticalSeekBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0/100" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="平滑度" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <androidx.appcompat.widget.AppCompatSeekBar style="@style/CustomVerticalSeekBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0/100" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="相似度" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <androidx.appcompat.widget.AppCompatSeekBar style="@style/CustomVerticalSeekBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0/100" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绿校对" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <androidx.appcompat.widget.AppCompatSeekBar style="@style/CustomVerticalSeekBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0/100" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_bg" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:gravity="center" android:text="前景画面" android:textColor="@color/text_color" android:textSize="@dimen/text_12sp" /> <androidx.appcompat.widget.SwitchCompat android:id="@+id/switchFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" app:thumbTint="@color/text_color" app:trackTint="@color/switch_color" /> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_h" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:background="@mipmap/ic_v" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <LinearLayout android:id="@+id/llIMageFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="图片" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:id="@+id/ivImageFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:visibility="invisible" android:background="@mipmap/sel_l" /> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:id="@+id/llCameraFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="摄像头" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:id="@+id/ivCameraFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:visibility="invisible" android:background="@mipmap/sel_l" /> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:id="@+id/llVideoFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="视频" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:id="@+id/ivVideoFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:visibility="invisible" android:background="@mipmap/sel_l" /> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:id="@+id/llSlideFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="幻灯片" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:id="@+id/ivSlideFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:visibility="invisible" android:background="@mipmap/sel_l" /> </LinearLayout> </LinearLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rvContentFg" android:layout_width="match_parent" android:layout_height="wrap_content" /> <!-- <ImageView--> <!-- android:layout_width="wrap_content"--> <!-- android:layout_height="wrap_content"--> <!-- android:layout_marginTop="10dp"--> <!-- android:background="@mipmap/ic_add" />--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_bg" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:gravity="center" android:text="音频输入" android:textColor="@color/text_color" android:textSize="@dimen/text_12sp" /> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> <!-- <androidx.recyclerview.widget.RecyclerView--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="wrap_content"/>--> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:background="@mipmap/ic_add" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="请对着音频输入设备说话,检测声音" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_bg" /> <androidx.appcompat.widget.AppCompatSeekBar style="@style/CustomVerticalSeekBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_weight="1" /> </LinearLayout> </LinearLayout> </ScrollView> </LinearLayout> </LinearLayout> <SeekBar android:id="@+id/progressBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="0" android:visibility="gone" app:layout_constraintBottom_toTopOf="@+id/tvvv" tools:ignore="MissingConstraints" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginLeft="12dp" android:layout_marginRight="12dp" android:layout_weight="3.4" android:background="@mipmap/bg_ground2" android:orientation="vertical"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/nav_rlv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:orientation="horizontal" /> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </LinearLayout> 这个布局中的layerEditor添加全屏和非全屏效果
06-17
<?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"> <!-- 视频显示区域(保持16:9比例) --> <RelativeLayout android:id="@+id/video_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_above="@+id/control_panel" android:layout_alignParentTop="true"> <SurfaceView android:id="@+id/surface_view" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> <!-- 控制面板 --> <LinearLayout android:id="@+id/control_panel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="vertical" android:padding="8dp" android:background="#33000000"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/btn_connect" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="连接服务器" android:backgroundTint="#4CAF50"/> <Button android:id="@+id/btn_disconnect" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="断开连接" android:backgroundTint="#F44336"/> </LinearLayout> <TextView android:id="@+id/tv_status" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:text="状态:未连接" android:textColor="#FFFFFF"/> </LinearLayout> </RelativeLayout> 上述xml文件添加相应的android:id="@+id/btnStart android:id="@+id/btnStop android:id="@+id/tvStatus三项
09-21
1、编写一个在安卓下运行的5x5的乘法表。当点击左对齐按钮时乘法表左对齐,点击右对齐按钮时变成右对齐。左或右对齐时需要对齐屏幕的边缘。 2、每个TextView的背景色不相同。 3、每个TextView的宽度至少比文字宽度多出一个字符宽度。 4、第一列文字左对齐,第二列文字居中对齐,第三列文字右对齐,第四列等号对齐。 5、以上效果用两种方案来实现。 6、如果使用图片切换,图片中需要有姓名的水印。 7、标题栏中显示学号和姓名。 8、源码要求清晰、简洁,关键代码、类和方法有详尽注释,并在文档中列出。 9、Package名称为:com.姓名首字母缩写。 10、文档需配有与代码相符的功能模块图或流程图。 11、运行结果需截图,标题栏中显示学号的姓名。 12、电子版实验报告还需附java和布局xml代码。 13、实验心得不少于200字。 实现方法: 1、两张图片切换或隐藏(此方法需要在图片中添加自己名字的水印) 2、嵌套布局,对最外的布局对齐(静态和动态布局) 3、动态填充TextView 4、在两个布局文件间切换 5、TextView的setLayoutParams方法 6、Visible 7、TableLayout动态填空 8、TableLayout的android:collapseColumns="1"<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:padding="12dp" android:background="#F5F5F5" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 唯一标题 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="201-cfj 5×5 乘法表实验" android:textSize="20sp" android:gravity="center" android:background="#3F51B5" android:textColor="#FFFFFF" android:padding="10dp"/> <!-- 方案 A 控制栏 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="【方案A】图片+布局切换" android:textStyle="bold" android:layout_marginTop="10dp"/> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btn_a_left" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="向左"/> <Button android:id="@+id/btn_a_right" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="向右"/> </LinearLayout> <FrameLayout android:id="@+id/container_a" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dp"/> <!-- 方案 B 控制栏 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="【方案B】TableLayout 动态填充" android:textStyle="bold"/> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btn_b_left" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="向左"/> <Button android:id="@+id/btn_b_right" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="向右"/> </LinearLayout> <TableLayout android:id="@+id/table_b" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
10-22
<!-- 示例:播放/暂停按钮 --> <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
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import com.example.myapplication.databinding.ActivityMainBinding; import net.objecthunter.exp4j.Expression; import net.objecthunter.exp4j.ExpressionBuilder; public class MainActivity extends AppCompatActivity { private ActivityMainBinding binding; private String currentInput = ""; private String expression = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); // 设置所有按钮的点击监听 setButtonListeners(); } private void setButtonListeners() { // 所有按钮ID数组 int[] buttonIds = { R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9, R.id.btnAdd, R.id.btnSubtract, R.id.btnMultiply, R.id.btnDivide, R.id.btnDot, R.id.btnPercent, R.id.btnPlusMinus, R.id.btnClear, R.id.btnDelete, R.id.btnEquals }; View.OnClickListener listener = v -> { // 获取按钮文本(安全处理 null) String text = ((TextView) v).getText().toString(); if (v.getId() == R.id.btnClear) { currentInput = ""; expression = ""; binding.textViewHistory.setText(""); binding.editTextDisplay.setText("0"); } else if (v.getId() == R.id.btnDelete) { if (!currentInput.isEmpty()) { currentInput = currentInput.substring(0, currentInput.length() - 1); binding.editTextDisplay.setText(currentInput.isEmpty() ? "0" : currentInput); } } else if (v.getId() == R.id.btnEquals) { try { Expression exp = new ExpressionBuilder(expression.isEmpty() ? currentInput : expression).build(); double result = exp.evaluate(); long longResult = (long) result; String displayResult = (result == (double) longResult) ? String.valueOf(longResult) : String.valueOf(result); binding.textViewHistory.setText((expression.isEmpty() ? currentInput : expression) + " ="); binding.editTextDisplay.setText(displayResult); currentInput = displayResult; expression = ""; } catch (Exception e) { binding.editTextDisplay.setText("Error"); } } else if (v.getId() == R.id.btnPlusMinus) { if (!currentInput.isEmpty() && !currentInput.equals("0")) { if (currentInput.startsWith("-")) { currentInput = currentInput.substring(1); } else { currentInput = "-" + currentInput; } binding.editTextDisplay.setText(currentInput); } } else if (v.getId() == R.id.btnPercent) { if (!currentInput.isEmpty()) { double val = Double.parseDouble(currentInput) / 100; currentInput = String.valueOf(val); binding.editTextDisplay.setText(currentInput); } } else { // 处理数字和操作符 if (isOperator(text)) { if (!currentInput.isEmpty()) { expression = currentInput + text; binding.textViewHistory.setText(expression); currentInput = ""; } else if (!expression.isEmpty()) { expression = expression.replaceAll("[+\\-×÷]$", text); binding.textViewHistory.setText(expression); } } else { currentInput += text; binding.editTextDisplay.setText(currentInput); } } }; // 为每个按钮设置监听器 for (int id : buttonIds) { View btn = binding.getRoot().findViewById(id); // 或直接 binding.btn0 等 if (btn != null) { btn.setOnClickListener(listener); } } } private boolean isOperator(String str) { return "+".equals(str) || "-".equals(str) || "×".equals(str) || "÷".equals(str); } } <?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" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#F5F5F5" android:padding="8dp"> <!-- 历史表达式显示 --> <TextView android:id="@+id/textViewHistory" android:layout_width="0dp" android:layout_height="wrap_content" android:text="" android:textSize="16sp" android:textColor="#666666" android:gravity="end" android:padding="12dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <!-- 当前输入/结果显示 --> <EditText android:id="@+id/editTextDisplay" android:layout_width="0dp" android:layout_height="wrap_content" android:text="0" android:textSize="32sp" android:textColor="#000000" android:gravity="end" android:padding="16dp" android:background="@android:color/transparent" android:focusable="false" android:cursorVisible="false" app:layout_constraintTop_toBottomOf="@id/textViewHistory" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <!-- 按钮网格容器 --> <GridLayout android:layout_width="0dp" android:layout_height="wrap_content" android:columnCount="4" android:rowCount="5" android:useDefaultMargins="true" app:layout_constraintTop_toBottomOf="@id/editTextDisplay" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"> <!-- 第一行:清除、正负、百分比、除法 --> <Button android:id="@+id/btnClear" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="C" android:textSize="18sp" android:textColor="#FFFFFF" android:backgroundTint="#FF9500" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btnPlusMinus" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="+/-" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btnPercent" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="%" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btnDivide" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="÷" android:textSize="18sp" android:textColor="#FFFFFF" android:backgroundTint="#FF9500" style="?android:attr/buttonStyle" /> <!-- 第二行:7,8,9,乘法 --> <Button android:id="@+id/btn7" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="7" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btn8" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="8" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btn9" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="9" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btnMultiply" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="×" android:textSize="18sp" android:textColor="#FFFFFF" android:backgroundTint="#FF9500" style="?android:attr/buttonStyle" /> <!-- 第三行:4,5,6,减法 --> <Button android:id="@+id/btn4" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="4" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btn5" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="5" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btn6" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="6" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btnSubtract" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="-" android:textSize="18sp" android:textColor="#FFFFFF" android:backgroundTint="#FF9500" style="?android:attr/buttonStyle" /> <!-- 第四行:1,2,3,加法 --> <Button android:id="@+id/btn1" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="1" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btn2" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="2" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btn3" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="3" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btnAdd" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="+" android:textSize="18sp" android:textColor="#FFFFFF" android:backgroundTint="#FF9500" style="?android:attr/buttonStyle" /> <!-- 第五行:0,小数点,删除,等于 --> <Button android:id="@+id/btn0" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="2" android:layout_columnSpan="2" android:layout_margin="4dp" android:text="0" android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btnDot" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="." android:textSize="18sp" android:textColor="#000000" android:backgroundTint="#E0E0E0" style="?android:attr/buttonStyle" /> <ImageButton android:id="@+id/btnDelete" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:src="@drawable/ic_backspace" android:backgroundTint="#E0E0E0" android:contentDescription="Delete" style="?android:attr/buttonStyle" /> <Button android:id="@+id/btnEquals" android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:layout_margin="4dp" android:text="=" android:textSize="18sp" android:textColor="#FFFFFF" android:backgroundTint="#FF9500" style="?android:attr/buttonStyle" /> </GridLayout> </androidx.constraintlayout.widget.ConstraintLayout>使用android Studio进行开发仿 Windows10 计算器 计算器是日常生活中常用的工具,下面仿写Windows10系统自带的计算器。计算器的页面由8部分组成,利用权重垂直方向分配高度,使组件占满整个屏幕,见图 4.12。第1 部分由EditText组成,重心向右。第2部分由水平方向的LinearLayout组成,内部包含6个TextVicw,水平方向6等分,注意字体颜色。第3部分由水平方向的LinearLayout组成,内部包含 Button和 ImageButton,水平方向4等分,ImageButton用于展示无法文本输出的“删除按键”,Button用于展示可以文本输出的其余按键。第4至8部分与第3部分设计的思路一致。注意采用Background 设置按钮不同的背景色。
09-23
【电动汽车充电站有序充电调度的分散式优化】基于蒙特卡诺和拉格朗日的电动汽车优化调度(分时电价调度)(Matlab代码实现)内容概要:本文介绍了基于蒙特卡洛和拉格朗日方法的电动汽车充电站有序充电调度优化方案,重点在于采用分散式优化策略应对分时电价机制下的充电需求管理。通过构建数学模型,结合不确定性因素如用户充电行为和电网负荷波动,利用蒙特卡洛模拟生成大量场景,并运用拉格朗日松弛法对复杂问题进行分解求解,从而实现全局最优或近似最优的充电调度计划。该方法有效降低了电网峰值负荷压力,提升了充电站运营效率与经济效益,同时兼顾用户充电便利性。 适合人群:具备一定电力系统、优化算法和Matlab编程基础的高校研究生、科研人员及从事智能电网、电动汽车相关领域的工程技术人员。 使用场景及目标:①应用于电动汽车充电站的日常运营管理,优化充电负荷分布;②服务于城市智能交通系统规划,提升电网与交通系统的协同水平;③作为学术研究案例,用于验证分散式优化算法在复杂能源系统中的有效性。 阅读建议:建议读者结合Matlab代码实现部分,深入理解蒙特卡洛模拟与拉格朗日松弛法的具体实施步骤,重点关注场景生成、约束处理与迭代收敛过程,以便在实际项目中灵活应用与改进。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值