Layout _width ,Layout_height和heigh ,width 区别:

Layout,翻译为中文的意思是 布局,安排,版面设计。对于许多的组件的命令,都有Layout_x的区别,而许多的区别很明显,直接是Layout是相对于父容器(一般就是整个xml的布局)的改变,一个是相对于组件本身的改变。比较典型的就是gravity和layout_gravity,当然也有例外的 ,比如Layout_marginpanding

对于每个组件,可以独自的使用Layout _width ,layout_heigh直接的设置相对于父容器的大小,

设置为 wrap_content或者 match_parent. 但是heigh ,width不能设置这样设置,不能设置相对于父容器,否则会产生

error: Error: String types not allowed (at 'width' with value 'wrap_content')。

并且,一个组件可以只有Layout _width ,layout_height。但却不能只有heigh ,width,而没有Layout _width ,layout_height,因为那样的组件会看不到。如果你要使用heigh ,width的话,就要先设置Layout _width ,layout_height,把heigh ,width用来作为组件的微调使用。

Xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello talk"
        android:textColor="#482" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:height="80dp"
        android:text="hello talk"
        android:textColor="#810"
        android:width="80dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello talk"
        android:textColor="#482" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:height="80dp"
        android:text="hello talk"
        android:textColor="#810"
        android:width="80dp" />

	</LinearLayout>

截图:

可以看出来不是改变的字体,而是直接改变的组件本生,但在TextvView中,好像效果不是很明显。

我们知道,其实在设置宽高的时候,我们是可以直接的把宽高设置为定制为:xdp,在这方面,Layout _width ,layout_height或者heigh ,width一样都可以设置。

至于还有其他的区别,有待发现中,呵呵!!


去网上找了下相关的,有这种说法:

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

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

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


至于说layout的属性是针对文本的,在这个例子中没有得到很好证实,因为对文本设置了宽高为 80dp,可是也没啥效果啊。


  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,你可以在布局文件中添加两个 Button 控件,如下所示: ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/ipEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入服务器IP地址" /> <EditText android:id="@+id/portEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入服务器端口号" /> <Button android:id="@+id/connectButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="连接" /> <SeekBar android:id="@+id/seekBar_x" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:max="30" android:progress="0" /> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <SeekBar android:id="@+id/seekBar_y" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:max="30" android:progress="0" /> </ScrollView> <Button android:id="@+id/sendMessageButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送消息" /> <Button android:id="@+id/cancelButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="取消" /> <Button android:id="@+id/confirmButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确认" /> </LinearLayout> ``` 这样就在布局文件中添加了两个按钮:取消和确认。你可以根据需要修改按钮的文本和样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值