Android中的layout_width和width的区别

本文详细解析了Android中控件的width与layout_width属性的区别,并通过示例介绍了如何利用这些属性进行有效的布局设计。
转:http://blog.youkuaiyun.com/u014434080/article/details/50791182

在平时定义控件的高宽时一直都在使用layout_width,从没使用过width。但既然提供了width,就很想知道它俩的区别。于是查了一些资料。现在借鉴归纳如下。主要是给自己当笔记。

android:width 其实是定义控件上面的文本(TextView) 的宽度,当然这个宽度也是和 android:layout_width 配合起来作用的,如果 android:layout_width=”fill_parent” 的话,那么设置 android:width 是没有意义的

android:layout_width 其实是可以实现 android:width 的效果的,我觉得这应该是为什么在 android 实例中看不到有人用 android:width 的原因吧。

若还要讲讲两者的区别的话,那就是:
android:width 的值,一般是 “100dp” 这样的数值;
android:layout_width 的值,一般是”fill_parent”,”wrap_content”,”match_parent”.当然,它也可以像前者一样,设置数值的.

带”layout”的属性是指整个控件而言的,是与父控件之间的关系,如 layout_gravity 在父控件中的对齐方式, layout_margin 是级别相同的控件之间的间隙等等;

不带”layout” 的属性是指控件中文本的格式,如gravity是指文本的对齐方式等等,而其中文本的格式又受制约于它的控件在父控件中的属性.

借用一位大牛的示例:http://zhangcong170.iteye.com/blog/423173


<?xml version="1.0" encoding="utf-8"?>    
<!--     
    <LinearLayout>    
    线性版面配置,在这个标签中,所有元件都是按由上到下的排队排成的    
 -->    
<LinearLayout     
    xmlns:android="http://schemas.android.com/apk/res/android"    
    android:orientation="vertical"     
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent">    
   <!-- android:orientation="vertical" 表示竖直方式对齐    
        android:orientation="horizontal"表示水平方式对齐    
        android:layout_width="fill_parent"定义当前视图在屏幕上 可以消费的宽度,fill_parent即填充整个屏幕。    
        android:layout_height="wrap_content":随着文字栏位的不同 而改变这个视图的宽度或者高度。有点自动设置框度或者高度的意思    

        layout_weight 用于给一个线性布局中的诸多视图的重要度赋值。    

        所有的视图都有一个layout_weight值,默认为零,意思是需要显示    
        多大的视图就占据多大的屏幕空 间。若赋一个高于零的值,则将父视    
        图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight    
        值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布    
        局的layout_weight值中所占的比率而定。    

        举个例子:比如说我们在 水平方向上有一个文本标签和两个文本编辑元素。    
        该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。    
        如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分    
        在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个     
        文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2,    
        则剩余空间的三分之二分给第一个,三分之一分给第二个(数值越小,重要    
        度越高)。    
    -->    
    <LinearLayout    
    android:orientation="horizontal"    
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent"    
    android:layout_weight="1">    
    <TextView    
        android:text="red"    
        android:gravity="center_horizontal"    
        android:background="#aa0000"    
        android:layout_width="wrap_content"    
        android:layout_height="fill_parent"    
        android:layout_weight="1"/>    

    <TextView    
        android:text="green"    
        android:gravity="center_horizontal"    
        android:background="#00aa00"    
        android:layout_width="wrap_content"    
        android:layout_height="fill_parent"    
        android:layout_weight="1"/>    

    <TextView    
        android:text="blue"    
        android:gravity="center_horizontal"    
        android:background="#0000aa"    
        android:layout_width="wrap_content"    
        android:layout_height="fill_parent"    
        android:layout_weight="1"/>    

    <TextView    
        android:text="yellow"    
        android:gravity="center_horizontal"    
        android:background="#aaaa00"    
        android:layout_width="wrap_content"    
        android:layout_height="fill_parent"    
        android:layout_weight="1"/>    

    </LinearLayout>    

    <LinearLayout    
    android:orientation="vertical"    
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent"    
    android:layout_weight="2">    

    <TextView    
        android:text="row one"    
        android:textSize="15pt"    
        android:layout_width="fill_parent"    
        android:layout_height="wrap_content"    
        android:layout_weight="1"/>    

    <TextView    
        android:text="row two"    
        android:textSize="15pt"    
        android:layout_width="fill_parent"    
        android:layout_height="wrap_content"    
        android:layout_weight="1"/>    

    <TextView    
        android:text="row three"    
        android:textSize="15pt"    
        android:layout_width="fill_parent"    
        android:layout_height="wrap_content"    
        android:layout_weight="1"/>    

    <TextView    
        android:text="row four"    
        android:textSize="15pt"    
        android:layout_width="fill_parent"    
        android:layout_height="wrap_content"    
        android:layout_weight="1"/>    

    </LinearLayout>    

</LinearLayout>  

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> <LinearLayout android:id="@+id/MainPage" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <RelativeLayout android:id="@+id/SerialContent" android:layout_width="match_parent" android:layout_height="60dp"> <TextView android:id="@+id/Serial" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="串口号" /> <Spinner android:id="@+id/SerialName" android:layout_width="100dp" android:layout_height="80dp" android:gravity="center" android:spinnerMode="dropdown" android:layout_toRightOf="@+id/Serial"/> <Spinner android:id="@+id/BaudRate" android:layout_width="140dp" android:layout_height="80dp" android:layout_toRightOf="@id/SerialName" android:gravity="center" android:spinnerMode="dropdown" /> <Button android:id="@+id/OpenSerial" android:layout_width="120dp" android:layout_height="match_parent" android:layout_toRightOf="@id/BaudRate" android:text="打开串口" /> <Button android:id="@+id/QuitSoftware" android:layout_width="140dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:text="退出软件" /> </RelativeLayout> <FrameLayout android:id="@+id/previewContent" android:layout_width="604.7dp" android:layout_height="430dp" android:visibility="gone"> <androidx.camera.view.PreviewView android:id="@+id/preview" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> <RelativeLayout android:id="@+id/handleContent" android:layout_width="match_parent" android:layout_height="430dp" android:visibility="visible"> <TextView android:id="@+id/handle" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="手柄" /> <LinearLayout android:id="@+id/RightContent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toRightOf="@+id/handle" android:orientation="vertical"> <RelativeLayout android:id="@+id/content1" android:layout_width="match_parent" android:layout_height="60dp" android:layout_toRightOf="@+id/handle"> <Button android:id="@+id/ChangeDial" android:layout_width="140dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:text="带拨号盘版" /> </RelativeLayout> <LinearLayout android:id="@+id/DialContent" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <EditText android:id="@+id/receivedDataView" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:drawable/editbox_background" android:focusable="false" android:focusableInTouchMode="false" android:cursorVisible="false" android:gravity="top|left" android:inputType="textMultiLine" android:padding="8dp" android:scrollbars="vertical" android:hint="等待数据..." android:textSize="14sp" /> <LinearLayout android:id="@+id/mianBoard" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <LinearLayout android:id="@+id/control" android:layout_width="match_parent" android:layout_height="74dp" android:orientation="horizontal" android:visibility="visible"> <EditText android:id="@+id/commandInput" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:hint="输入指令" android:paddingLeft="10dp" android:inputType="text" android:maxLines="1" /> <Button android:id="@+id/sendButton" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="发送" /> <Button android:id="@+id/clearDataView" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="清空" /> </LinearLayout> <GridLayout android:id="@+id/keyBoard" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="4" android:columnCount="3" android:rowCount="4" android:visibility="visible"> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="1" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="2" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="3" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="4" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="5" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="6" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="7" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="8" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="9" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="*" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="0" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="#" /> </GridLayout> <GridLayout android:id="@+id/NoKeyBoard" android:layout_width="match_parent" android:layout_height="0dp" android:columnCount="6" android:rowCount="4" android:layout_weight="5" android:visibility="gone"> <TextView android:id="@+id/keyA" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEYA" android:gravity="center"/> <Button android:id="@+id/keyAStart" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1.2" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/keyAClose" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1.2" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/keyB" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEYB" android:gravity="center"/> <Button android:id="@+id/keyBStart" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1.2" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/keyBClose" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1.2" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/key1" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEY1" android:gravity="center"/> <Button android:id="@+id/key1Start" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/key1Close" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/key2" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEY2" android:gravity="center"/> <Button android:id="@+id/key2Start" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/key2Close" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/key3" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEY3" android:gravity="center"/> <Button android:id="@+id/key3Start" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/key3Close" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/key4" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEY4" android:gravity="center"/> <Button android:id="@+id/key4Start" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/key4Close" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/key5" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEY5" android:gravity="center"/> <Button android:id="@+id/key5Start" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/key5Close" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="关灯" /> </GridLayout> </LinearLayout> </LinearLayout> </LinearLayout> </RelativeLayout> <RelativeLayout android:id="@+id/VoiceContent" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:id="@+id/voice" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="录音" /> <Button android:id="@+id/RecordingVoice" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:layout_toRightOf="@+id/voice" android:text="开始录音" /> <TextView android:id="@+id/ShowVoiceData" android:layout_width="150dp" android:layout_height="match_parent" android:layout_toRightOf="@+id/RecordingVoice" android:background="@drawable/solid_shape" android:gravity="center" android:ellipsize="end" android:singleLine="false" android:maxLines="3"/> <Button android:id="@+id/PlayVoice" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:layout_toRightOf="@id/ShowVoiceData" android:text="播放语音" /> <Button android:id="@+id/DeleteAudioAndView" android:layout_width="150dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_margin="2dp" android:text="删除" /> </RelativeLayout> <RelativeLayout android:id="@+id/CameraContent" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:id="@+id/camera" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="摄像头" /> <Button android:id="@+id/OpenAndCloseCamera" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:layout_toRightOf="@+id/camera" android:text="打开摄像头" /> <Button android:id="@+id/photograph" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:layout_toRightOf="@id/OpenAndCloseCamera" android:text="拍照" /> <TextView android:id="@+id/PhotographData" android:layout_width="150dp" android:layout_height="match_parent" android:layout_toRightOf="@+id/photograph" android:background="@drawable/solid_shape" android:gravity="center" android:ellipsize="end" android:singleLine="false" android:maxLines="3"/> <Button android:id="@+id/RecordingView" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:layout_toRightOf="@+id/PhotographData" android:text="录制视频" /> <TextView android:id="@+id/RecordingViewData" android:layout_width="150dp" android:layout_height="match_parent" android:layout_toRightOf="@+id/RecordingView" android:background="@drawable/solid_shape" android:gravity="center" android:ellipsize="end" android:singleLine="false" android:maxLines="3"/> <Button android:id="@+id/DeleteView" android:layout_width="150dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_margin="2dp" android:text="删除" /> </RelativeLayout> <LinearLayout android:id="@+id/ScreenTestContent" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:id="@+id/ScreenTest" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="屏幕测试" /> <Button android:id="@+id/ScreenCheck" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="屏幕检测" /> <TextView android:id="@+id/TouchScreenCheck" android:layout_width="150dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="触摸屏检测" /> <Button android:id="@+id/ScribingTest" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="划线测试" /> <Button android:id="@+id/SinglePointTest" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="单点测试" /> </LinearLayout> <LinearLayout android:id="@+id/NetPortContent" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:id="@+id/NetPort" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="网口" /> <TextView android:id="@+id/NetPortData" android:layout_width="150dp" android:layout_height="match_parent" android:ellipsize="end" android:layout_margin="2dp" android:gravity="center" android:background="@drawable/solid_shape"/> <TextView android:id="@+id/ping" android:layout_width="150dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="ping" /> <EditText android:id="@+id/pingInput" android:layout_width="150dp" android:layout_height="match_parent" android:hint="请输入" android:paddingLeft="10dp" android:text="192.168.1.1" android:layout_margin="2dp" android:inputType="text" android:maxLines="1"/> <Button android:id="@+id/pingSend" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="发送ping"/> <ScrollView android:id="@+id/scrollView" android:layout_width="200dp" android:layout_height="match_parent" android:fillViewport="true"> <TextView android:id="@+id/pingData" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/solid_shape" android:textSize="14sp" android:singleLine="false" android:maxLines="100" android:scrollbars="vertical" android:layout_margin="2dp"/> </ScrollView> <Button android:id="@+id/pingStop" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="停止"/> </LinearLayout> <RelativeLayout android:id="@+id/USBContent" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:id="@+id/USB" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="USB接口" /> <TextView android:id="@+id/USBPath" android:layout_width="150dp" android:layout_height="match_parent" android:hint="未连接" android:ellipsize="end" android:layout_margin="2dp" android:gravity="center" android:background="@drawable/solid_shape" android:layout_toRightOf="@+id/USB"/> <Button android:id="@+id/getUSBPath" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="获取USB路径" android:layout_toRightOf="@+id/USBPath"/> <EditText android:id="@+id/WriteTextData" android:layout_width="150dp" android:layout_height="match_parent" android:inputType="text" android:paddingLeft="10dp" android:layout_toRightOf="@+id/getUSBPath"/> <Button android:id="@+id/WriteText" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="写入文本" android:layout_toRightOf="@+id/WriteTextData"/> <ScrollView android:id="@+id/scrollViewOpenTextData" android:layout_width="200dp" android:layout_height="match_parent" android:fillViewport="true" android:layout_toRightOf="@+id/WriteText"> <TextView android:id="@+id/OpenTextData" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/solid_shape" android:textSize="14sp" android:singleLine="false" android:maxLines="100" android:scrollbars="vertical" android:layout_margin="2dp" android:gravity="center"/> </ScrollView> <Button android:id="@+id/OpenText" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="打开文本" android:layout_toRightOf="@+id/scrollViewOpenTextData"/> <Button android:id="@+id/DeleteText" android:layout_width="150dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_margin="2dp" android:text="删除" /> </RelativeLayout> </LinearLayout> </ScrollView>使以上代码实现自适应屏幕大小,给出全部完整的代码
最新发布
08-14
为了使你提供的 XML 布局文件实现**完整的自适应屏幕适配**,我们需要: - 将整个布局结构从 `LinearLayout` + `RelativeLayout` 改为 **`ConstraintLayout`** - 替换所有固定尺寸(如 `160dp`、`604.7dp`)为 **`0dp` + 百分比约束** - 使用 `layout_constraintWidth_percent` 实现宽度比例分配 - 使用 `layout_weight` 在 `LinearLayout` 中分配剩余空间(如输入框按钮) - 使用 `androidx.core.widget.Space` 替代固定 `margin` - 使用 `dimens.xml` 定义通用尺寸,便于适配不同屏幕 - 保留 `ScrollView` 实现滚动功能 --- ## ✅ 最终完整自适应布局代码(使用 `ConstraintLayout`) 以下是**完整自适应布局代码**,你可以直接复制到你的 `activity_main.xml` 文件中。 ```xml <?xml version="1.0" encoding="utf-8"?> <ScrollView 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:fillViewport="true"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/MainPage" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp"> <!-- 串口部分 --> <TextView android:id="@+id/Serial" android:layout_width="0dp" android:layout_height="48dp" android:background="@drawable/form_shape" android:gravity="center" android:text="串口号" app:layout_constraintWidth_percent="0.25" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" /> <Spinner android:id="@+id/SerialName" android:layout_width="0dp" android:layout_height="48dp" android:gravity="center" app:layout_constraintWidth_percent="0.2" app:layout_constraintStart_toEndOf="@id/Serial" app:layout_constraintTop_toTopOf="parent" /> <Spinner android:id="@+id/BaudRate" android:layout_width="0dp" android:layout_height="48dp" android:gravity="center" app:layout_constraintWidth_percent="0.25" app:layout_constraintStart_toEndOf="@id/SerialName" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/OpenSerial" android:layout_width="0dp" android:layout_height="48dp" android:text="打开串口" app:layout_constraintWidth_percent="0.15" app:layout_constraintStart_toEndOf="@id/BaudRate" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/QuitSoftware" android:layout_width="0dp" android:layout_height="48dp" android:text="退出软件" app:layout_constraintWidth_percent="0.15" app:layout_constraintStart_toEndOf="@id/OpenSerial" app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <!-- 预览区域 --> <FrameLayout android:id="@+id/previewContent" android:layout_width="0dp" android:layout_height="200dp" android:visibility="gone" app:layout_constraintWidth_percent="0.9" app:layout_constraintTop_toBottomOf="@id/Serial" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"> <androidx.camera.view.PreviewView android:id="@+id/preview" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> <!-- 手柄部分 --> <TextView android:id="@+id/handle" android:layout_width="0dp" android:layout_height="48dp" android:background="@drawable/form_shape" android:gravity="center" android:text="手柄" app:layout_constraintWidth_percent="0.25" app:layout_constraintTop_toBottomOf="@id/previewContent" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/ChangeDial" android:layout_width="0dp" android:layout_height="48dp" android:text="带拨号盘版" app:layout_constraintWidth_percent="0.25" app:layout_constraintTop_toBottomOf="@id/previewContent" app:layout_constraintStart_toEndOf="@id/handle" app:layout_constraintEnd_toEndOf="parent" /> <!-- 输入框部分 --> <EditText android:id="@+id/commandInput" android:layout_width="0dp" android:layout_height="48dp" android:hint="输入指令" app:layout_constraintWidth_percent="0.33" app:layout_constraintTop_toBottomOf="@id/handle" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/sendButton" android:layout_width="0dp" android:layout_height="48dp" android:text="发送" app:layout_constraintWidth_percent="0.33" app:layout_constraintTop_toBottomOf="@id/handle" app:layout_constraintStart_toEndOf="@id/commandInput" /> <Button android:id="@+id/clearDataView" android:layout_width="0dp" android:layout_height="48dp" android:text="清空" app:layout_constraintWidth_percent="0.33" app:layout_constraintTop_toBottomOf="@id/handle" app:layout_constraintStart_toEndOf="@id/sendButton" app:layout_constraintEnd_toEndOf="parent" /> <!-- 按键盘 --> <GridLayout android:id="@+id/keyBoard" android:layout_width="0dp" android:layout_height="0dp" android:columnCount="3" android:rowCount="4" app:layout_constraintWidth_percent="0.5" app:layout_constraintHeight_percent="0.3" app:layout_constraintTop_toBottomOf="@id/commandInput" app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent"> <!-- 按钮行 --> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="1" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="2" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="3" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="4" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="5" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="6" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="7" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="8" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="9" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="*" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="0" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="#" /> </GridLayout> <!-- 更多模块可继续添加 --> </androidx.constraintlayout.widget.ConstraintLayout> </ScrollView> ``` --- ## ✅ 使用的资源文件 ### ✅ `res/values/dimens.xml` ```xml <resources> <dimen name="standard_margin">16dp</dimen> <dimen name="button_height">48dp</dimen> <dimen name="section_padding">8dp</dimen> </resources> ``` ### ✅ `res/drawable/form_shape.xml` ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF" /> <stroke android:width="1dp" android:color="#CCCCCC" /> <corners android:radius="4dp" /> </shape> ``` ### ✅ `res/drawable/solid_shape.xml` ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#EEEEEE" /> </shape> ``` --- ## ✅ 自适应布局技巧总结 | 技术 | 说明 | |------|------| | `ConstraintLayout` | 替代嵌套布局,提升性能灵活性 | | `0dp + layout_constraintWidth_percent` | 设置控件宽度占屏幕宽度比例 | | `layout_weight` | 在 `LinearLayout` 中实现比例分配 | | `Space` | 替代固定 margin,提升适配灵活性 | | `dimens.xml` | 定义通用尺寸,便于多屏幕适配 | | `ScrollView + ConstraintLayout` | 实现可滚动的复杂布局 | --- ## ✅ 相关问题 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值