关于Android UI组件LinearLayout属性layout_weight与layout_width/height的问题

本文深入解析了Android中LinearLayout的layout_weight属性工作原理,包括其在不同情况下的表现及计算方法,并提供实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://hi.baidu.com/wei_chou/item/04b51be1abb1e316595dd853

在网上搜索了很多关于layout_weight的文章,众说纷纭,且都不准确。后来自己动手测试,通过分析计算得出以下结论:

1、如果LinearLayout在其子组件相应排列方向上的大小值(layout_width/height)为wrap_content,则忽略所有子组件的layout_weight,且相应方向上的大小值也替换为wrap_content。例如:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android=" http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="Button01"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Button02"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Button03"/>
</LinearLayout>

由于LinearLayout的android:orientation="horizontal",子组件水平排列,而android:layout_width="wrap_content",所有将忽略所有子组件的layout_weight,并将android:layout_width值替换为wrap_content。垂直方向同理。
2、layout_weight值表示该组件应该增加或减少的值占剩余空间的比例,没有优先级之说。至于为什么有的组件会不显示,这不是因为优先级的原因。通过以下公式的计算,你会明白。本人愚钝,拟公式如下:
W=W1+L*P;
其中W表示最终宽度或高度,W1表示第一次测量的宽度/高度,L表示剩余空间的值(可能为负数),P表示根据android:layout_weight属性值计算出来的百分比。
将上面代码更改如下(注意android:orientation跟android:layout_width的对应关系),此时子组件的layout_width/height和layout_weight将被应用:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android=" http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Button01"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button02"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="Button03"/>
</LinearLayout>
android对组件进行布局的时候会组件大小进行两次计算:第一次是测量没有应用android:layout_weight时的值,第二次是根据第一次测量值重新计算组件应用android:layout_weight之后的实际值。
设LinearLayout父组件宽度为w,由于三个Button的layout_width值都为wrap_content,为简单起见,设第一次测量三个Button的宽度都为W1=w/5,合起来的总宽度为total_w=3w/5。则剩余空间的大小为L=w-total_w=2w/5,下面进行第二次计算,分别对三个Button的实际大小进行计算:
Button01:
P=3/(3+2+5)=0.3;
W1=w/5=0.2w;
W=W1+L*P=w/5+2w/5*0.3=0.32w;//增加了0.12w

Button02:
P=2/(3+2+5)=0.2;
W1=w/5=0.2w;
W=W1+L*P=w/5+2w/5*0.2=0.28w;//增加了0.08w

Button03:
P=5/(3+2+5)=0.5;
W1=w/5=0.2w;
W=W1+L*P=w/5+2w/5*0.5=0.4w;//增加了0.2w
结果显而易见。
那如果三个Button的layout_width值都为fill_parent会怎样呢?此时
W1=w;
L=w-total_w=w-3*W1=-2w;
Button01:
P=3/(3+2+5)=0.3;
W1=w;
W=W1+L*P=w+(-2w)*0.3=0.4w;//减少了0.6w

Button02:
P=2/(3+2+5)=0.2;
W1=w;
W=W1+L*P=w+(-2w)*0.2=0.6w;//减少了0.6w

Button03:
P=5/(3+2+5)=0.5;
W1=w;
W=W1+L*P=w+(-2w)*0.5=0;//宽度为0,所以不显示
由于Button03的宽度为0,所以这时只能显示Button01和Button02。而不是因为其layout_weight值比较大,优先级低导致的。
到这里是不是突然觉得很简单啊!
如果Button03的layout_weight改为7呢?自己去算吧,W将小于0,因此也不会显示。
还有一种情况,如果设Button02的layout_width="wrap_content",其他都为fill_parent呢?计算方法一样。
垂直方向同理。
另外当android:orientation="horizontal"时,垂直方向如何应用?首先适用本文第一条,否则当LinearLayout的android:layout_height="fill_parent"时,若子组件的android:layout_height="fill_parent",则纵向填满,否则自适应大小。
补充说明之妙用:

3、若不设置LinearLayout的weightSum以及所有子组件的layout_weight值,即两者的值都为0,则将要出界的组件的尺寸将会被调整。规则如下:所有值为
wrap_content fill_parent 的组件,若按其正常尺寸,有部分在父组件边界内而部分在父组件边界外的,则将其尺寸调整到正好容纳在边界之内;完全在边界外部的组件将会隐藏,即宽或高为0;所有值不为wrap_content fill_parent之外 的组件,即有固定尺寸的,如100dp,则无论在边界内外都按固定尺寸显示。
问题:我不想给子组件设置固定的尺寸,也不想出界的组件会隐藏,而是想当我滚动内容的时候scrollTo(x,y)/scrollBy(x,y),出界的部分会显示出来而不是依然隐藏。根据前面的第1、2条,我们知道应用weight可以让组件出界,但却会改变组件的大小。其实有简单的办法,见下一条。
4、若LinearLayout的weightSum和子组件的layout_weight有一个不为0,当依次应用子组件的layout_weight来进行计算组件尺寸的时候(见第2条),当某子组件与其前面的所有子组件的layout_weight值之和正好等于weightSum的时候,则忽略其后所有子组件的layout_weight属性,此时,其后的所有子组件将保持原始的尺寸。

根据本条特性,解决上面的问题很容易了,见代码:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android=" http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_width="0px"
android:layout_height="0px"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="400dp"
android:text="按钮0"/>
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="按钮1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="按钮2"/>
</LinearLayout>
该布局中,由于View的android:layout_weight正好等于android:weightSum(这里省略了,因为android:weightSum默认为所有子组件的android:layout_weight的值之和),所有的Button都会保持其原始的大小并出界。
读者可以自行测试其他情况,将android:weightSum和各子组件的android:layout_weight设置不同的值,看看效果。
3、4条为后来补充。到这里算是完善了。
补充:更为简单且规范的实现组件出界的方法:

重写onMeasure()方法,调整参数,将测量模式设置为MeasureSpec.UNSPECIFIED
@Override
protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
widthMeasureSpec=MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
}
详见我的另一篇文章,对测量(measure)的详细解说。

根据上述功能,优化包含边框的悬浮窗布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:clipChildren="true" android:clipToPadding="true" android:orientation="vertical"> <View android:id="@+id/drag_top" android:layout_width="wrap_content" android:layout_height="14dp" android:visibility="gone" android:layout_gravity="top" android:background="@color/milu_orange" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <View android:id="@+id/drag_left" android:layout_width="14dp" android:layout_height="match_parent" android:visibility="gone" android:background="@color/milu_orange" /> <LinearLayout android:id="@+id/content_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:paddingBottom="10dp"> <LinearLayout android:id="@+id/ll_draggable" android:layout_width="25dp" android:layout_height="25dp" android:layout_centerVertical="true" android:gravity="center"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ml_chat_draggable" /> </LinearLayout> <TextView android:layout_width="43dp" android:layout_height="3dp" android:layout_centerInParent="true" android:background="@drawable/botton_yuan_ef_5" /> <LinearLayout android:id="@+id/ll_close" android:layout_width="25dp" android:layout_height="25dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:gravity="center"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ml_bjlb_close" /> </LinearLayout> </RelativeLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rlv_messagr" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="5" android:scrollbars="none" /> <TextView android:layout_width="wrap_content" android:layout_height="0dp" android:background="@drawable/bg_redius_transparent_75" android:paddingHorizontal="15dp" android:layout_weight="1" android:paddingVertical="5dp" android:text="说点什么吧" android:textColorHint="@color/milu_color_99" /> </LinearLayout> <View android:id="@+id/drag_right" android:layout_width="14dp" android:layout_height="match_parent" android:visibility="gone" android:background="@color/milu_orange" /> </LinearLayout> <LinearLayout android:id="@+id/ll_bottom" android:layout_width="match_parent" android:layout_height="14dp" android:visibility="gone" android:layout_gravity="left|bottom" android:orientation="horizontal"> <View android:id="@+id/drag_bottom_left" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/milu_orange" /> <View android:id="@+id/drag_bottom_right" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/milu_orange" /> </LinearLayout> </LinearLayout>
最新发布
07-03
<?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
MainActivity:package com.videogo.ui.login; import android.content.res.Configuration; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import com.videogo.exception.BaseException; import com.videogo.exception.ErrorCode; import com.videogo.openapi.EZConstants; import com.videogo.openapi.EZOpenSDK; import com.videogo.openapi.EZPlayer; import ezviz.ezopensdk.R; public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, Handler.Callback { private static final String TAG = "EZPreview"; private static final int MSG_VIDEO_SIZE_CHANGED = 1; private static final int MSG_REALPLAY_PLAY_SUCCESS = 2001; private static final int MSG_REALPLAY_PLAY_FAIL = 2002; // 接收的参数键 private static final String KEY_APPKEY = "appkey"; private static final String KEY_SERIAL = "serial"; private static final String KEY_VERIFYCODE = "VerifyCode"; private static final String KEY_ACCESSTOKEN = "accessToken"; private static final String KEY_CAMERANO = "cameraNo"; private EZPlayer mEZPlayer; private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; // 从Intent中获取的参数 private String mAppKey; private String mDeviceSerial; private String mVerifyCode; private String mAccessToken; private int mCameraNo = 0; // 默认通道号0 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activitymain); // 1. 从Intent中获取参数 extractParametersFromIntent(); // 2. 初始化UI initUI(); // 3. 初始化SDK并创建播放器 initSDKAndCreatePlayer(); } /** * 从Intent中提取传递的参数 */ private void extractParametersFromIntent() { Bundle extras = getIntent().getExtras(); if (extras != null) { mAppKey = extras.getString(KEY_APPKEY, ""); mDeviceSerial = extras.getString(KEY_SERIAL, ""); mVerifyCode = extras.getString(KEY_VERIFYCODE, ""); mAccessToken = extras.getString(KEY_ACCESSTOKEN, ""); mCameraNo = extras.getInt(KEY_CAMERANO, 0); Log.d(TAG, "Received parameters:"); Log.d(TAG, "AppKey: " + mAppKey); Log.d(TAG, "DeviceSerial: " + mDeviceSerial); Log.d(TAG, "VerifyCode: " + mVerifyCode); Log.d(TAG, "AccessToken: " + mAccessToken); Log.d(TAG, "CameraNo: " + mCameraNo); } else { Log.e(TAG, "No parameters received from intent"); // 如果没有参数,可以显示错误信息并退出 finish(); } } /** * 初始化UI组件 */ private void initUI() { mSurfaceView = findViewById(R.id.realplay_sv); if (mSurfaceView != null) { mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.addCallback(this); } else { Log.e(TAG, "SurfaceView not found with ID realplay_sv"); } } /** * 初始化SDK并创建播放器 */ private void initSDKAndCreatePlayer() { try { // 1. 初始化SDK EZOpenSDK.initLib(getApplication(), mAppKey); EZOpenSDK.getInstance().setAccessToken(mAccessToken); // 2. 创建播放器 createPlayer(); } catch (Exception e) { Log.e(TAG, "SDK initialization failed", e); } } /** * 创建播放器并开始播放 */ private void createPlayer() { try { // 1. 创建播放器实例 mEZPlayer = EZOpenSDK.getInstance().createPlayer(mDeviceSerial, mCameraNo); // 2. 配置播放器 mEZPlayer.setHandler(new Handler(this)); if (mSurfaceHolder != null) { mEZPlayer.setSurfaceHold(mSurfaceHolder); } if (mVerifyCode != null && !mVerifyCode.isEmpty()) { mEZPlayer.setPlayVerifyCode(mVerifyCode); } // 3. 开始播放 mEZPlayer.startRealPlay(); } catch (Exception e) { Log.e(TAG, "Player creation failed", e); } } // 处理屏幕旋转按钮点击 public void changeScreen(View view) { Log.d(TAG, "Change screen orientation requested"); // 实际切换屏幕方向的代码 int currentOrientation = getResources().getConfiguration().orientation; if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } } // Surface回调接口实现 @Override public void surfaceCreated(@NonNull SurfaceHolder holder) { if (mEZPlayer != null) { mEZPlayer.setSurfaceHold(holder); } } @Override public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {} @Override public void surfaceDestroyed(@NonNull SurfaceHolder holder) { if (mEZPlayer != null) { mEZPlayer.setSurfaceHold(null); } } @Override protected void onStop() { super.onStop(); if (mEZPlayer != null) { mEZPlayer.stopRealPlay(); } } @Override protected void onDestroy() { super.onDestroy(); if (mEZPlayer != null) { mEZPlayer.release(); mEZPlayer = null; } } // Handler回调处理播放状态 @Override public boolean handleMessage(@NonNull Message msg) { Log.d(TAG, "handleMessage: " + msg.what); switch (msg.what) { case MSG_VIDEO_SIZE_CHANGED: // 视频尺寸变化,可以调整SurfaceView的布局 break; case MSG_REALPLAY_PLAY_SUCCESS: Log.i(TAG, "播放成功"); break; case MSG_REALPLAY_PLAY_FAIL: Log.e(TAG, "播放失败"); BaseException error = (BaseException) msg.obj; int errorCode = error.getErrorCode(); if (errorCode == ErrorCode.ERROR_INNER_VERIFYCODE_NEED || errorCode == ErrorCode.ERROR_INNER_VERIFYCODE_ERROR) { // 处理验证码错误 // 这里应该提示用户输入验证码,然后重新设置并播放 // 示例中暂时保留模拟输入验证码 mVerifyCode = "123456"; if (mEZPlayer != null) { mEZPlayer.setPlayVerifyCode(mVerifyCode); mEZPlayer.startRealPlay(); } } else { Log.e(TAG, "播放失败,错误码: " + errorCode); } break; } return true; } } activity.xml:<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff2f2f2" tools:context="com.videogo.ui.login.MainActivity"> <RelativeLayout android:id="@+id/ra_title" android:layout_width="match_parent" android:layout_height="44dp" android:background="@mipmap/title_bg"> <TextView android:id="@+id/titlename" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_gravity="center" android:text="易丰视频监控" android:textColor="@android:color/white" android:textSize="18sp" /> <ImageButton android:id="@+id/back" android:layout_width="40dp" android:layout_height="match_parent" android:background="@null" android:contentDescription="@null" android:paddingLeft="10dp" android:src="@mipmap/img_back" /> <ImageButton android:id="@+id/ib_rotate" android:layout_width="50dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:contentDescription="@null" android:onClick="changeScreen" android:layout_marginRight="6dp" android:scaleType="centerInside" android:background="@drawable/gps_select" android:src="@mipmap/ic_size_sel" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_control" android:layout_width="match_parent" android:layout_height="266dp" android:layout_alignParentBottom="true" > <LinearLayout android:id="@+id/ll_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerInParent="true" android:background="@mipmap/ycjk_yp" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ycjk_kb1" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ycjk_kb2" /> <ImageButton android:visibility="gone" android:id="@+id/left_up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ycjk_kb3" /> </LinearLayout> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ycjk_kb4" /> </LinearLayout> <ImageButton android:id="@+id/ptz_top_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/nnew_video_up" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ycjk_kb1" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ycjk_kb3" /> <ImageButton android:visibility="gone" android:id="@+id/right_up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ycjk_kb2" /> </LinearLayout> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ycjk_kb4" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" > <ImageButton android:id="@+id/ptz_left_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/nnew_video_left" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ycjk_zj" /> <ImageButton android:id="@+id/ptz_right_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/nnew_video_right" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ycjk_kb4" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ycjk_kb2" /> <ImageButton android:visibility="gone" android:id="@+id/left_down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ycjk_kb3" /> </LinearLayout> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ycjk_kb1" /> </LinearLayout> <ImageButton android:id="@+id/ptz_bottom_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/nnew_video_down" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ycjk_kb4" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ycjk_kb3" /> <ImageButton android:visibility="gone" android:id="@+id/right_down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ycjk_kb2" /> </LinearLayout> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ycjk_kb1" /> </LinearLayout> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toLeftOf="@id/ll_center" android:gravity="center" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <ImageButton android:id="@+id/focus_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/video_more1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:textSize="16dp" android:text="焦距 +" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/guangquan_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/video_more3" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:text="光圈 +" android:textSize="16dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/zoom_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/video_more5" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:text="变倍 +" android:textSize="16dp" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toRightOf="@id/ll_center" android:gravity="center" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/foucus_reduce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/video_more2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:text="焦距 -" android:textSize="16dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/guangquan_reduce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/video_more4" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:text="光圈 -" android:textSize="16dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/zoom_reduce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/video_more6" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:text="变倍 -" android:textSize="16dp" /> </LinearLayout> </LinearLayout> </RelativeLayout> <RelativeLayout android:layout_above="@id/rl_control" android:layout_below="@id/ra_title" android:layout_width="match_parent" android:layout_height="match_parent" > <SurfaceView android:id="@+id/realplay_sv" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" /> <ImageButton android:id="@+id/ib_rotate2" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParentLeft="true" android:layout_marginLeft="3dp" android:background="@color/green" android:contentDescription="@null" android:onClick="changeScreen" android:src="@mipmap/img_systems_close" /> <ProgressBar android:id="@+id/liveProgressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <LinearLayout android:id="@+id/ll_hc" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" > <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" > <ImageButton android:id="@+id/ptz_top_btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/h_up" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" > <ImageButton android:id="@+id/ptz_bottom_btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/h_down" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" > <ImageButton android:id="@+id/ptz_left_btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/h_left" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" > <ImageButton android:id="@+id/ptz_right_btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/h_right" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" > <ImageButton android:id="@+id/focus_add2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/video_more1" /> </LinearLayout> <LinearLayout android:gravity="center" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <ImageButton android:id="@+id/foucus_reduce2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/video_more2" /> </LinearLayout> <LinearLayout android:gravity="center" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <ImageButton android:id="@+id/zoom_add2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/video_more5" /> </LinearLayout> <LinearLayout android:gravity="center" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <ImageButton android:id="@+id/zoom_reduce2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/video_more6" /> </LinearLayout> </LinearLayout> </RelativeLayout> </RelativeLayout> 在上述代码中设置当直播画面realplay_sv在开始播放的时候隐藏就android:id="@+id/liveProgressBar"这个加载图标,当手机是竖屏的时候隐藏android:id="@+id/ll_hc"这一模块中的8个按钮以及android:id="@+id/ib_rotate2"这一个按钮,其他正常显示。当手机是横屏的时候隐藏android:id="@+id/rl_control"这一模块所有布局,其他正常显示。
06-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值