Android LinearLayout线性布局(上下左右 正中间)示例

我先把整个窗口 进行了垂直对齐,把窗口分成了上中下三层,用到的是一个权重属性:android:layout_weight="1",就是按比例分配大小,上下设置为1,中间层设置8,然后再在每一层里面,对按钮进行布局,中间层里面还要嵌套一个线性布局,这是因为android:orientation="horizontal",这个布局方向的特点,默认是水平对齐,如果设置成水平对齐了,那么里面的元素就不能进行水平方向的修改(android:layout_gravity="center_horizontal"就没有效果),相反同理,所以就要用两层线性布局嵌套来解决。

下面是activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 

tools:context="${packageName}.${activityClass}" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="左上" />

<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="8"
android:text="2" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="右上" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="9"
android:orientation="horizontal" >

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="中间" />

</LinearLayout>
</LinearLayout>

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom" 
>
<Button 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="左下"
/>
<Button 
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="8"
android:text="2"
/>
<Button 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="右下"
/>

</LinearLayout>

</LinearLayout>



</LinearLayout>


  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
线性布局是一种常见的布局方式,在Android Studio中也可以使用线性布局来构建界面。要创建线性布局,可以按照以下步骤: 1. 打开Android Studio并创建一个新的Android项目。 2. 找到res文件夹,右键点击并选择New -> Android Resource File。 3. 在弹出的对话框中,输入布局文件的名称,选择Layout作为资源类型,然后点击OK。 4. 在XML文件中,使用LinearLayout标签来定义线性布局LinearLayout有两个主要的属性:orientation和layout_width/layout_height。 - orientation属性用于指定布局的方向,可以是垂直(vertical)或水平(horizontal)。 - layout_width和layout_height属性用于指定布局在父容器中的宽度和高度。 示例代码如下: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 在这里添加其他控件 --> </LinearLayout> ``` 5. 在LinearLayout标签内部,可以添加其他的控件,例如TextView、Button等。通过设置控件的layout_width和layout_height属性,可以控制它们在线性布局中的大小和位置。 例如,在LinearLayout内添加一个TextView和一个Button的示例代码如下: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me!" /> </LinearLayout> ``` 以上是使用线性布局创建界面的基本步骤,在实际使用中可以根据需要进行布局调整和添加其他的控件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值