網站首頁 編程語言 正文
概述
LinearLayout是線性布局組件,放置在其中的組件按列或者按行(就是垂直或者水平)的方式排序分布。
常用XML配置屬性
(1)?android:orientation
設置LinearLayout容器布局組件的方式:只能取值:horizontal(水平的),vertical(垂直的)
(2) android:gravity
設置布局在LinearLayout容器內的組件對齊方式。
取值包括top, bottom, left, right, center, start, end(上,下,左,右,中,開始,結束)
(3) View中繼承來的屬性
(包括android:background ,android:visibility等。還有一些改善美觀的放置組件的間隔)
1.?android:layout_width和android:layout_height (match_parent/wrap_content)
2 .android:layout_gravity? ?設置組件在容器中的布局?
3. android:layout_weight? 設置組件占用空間的空余顯示空間的比列
4.?android:layout_margin??,android:layout_marginTop??,android:layout_marginBottom??,android:layout_marginLeft??,android:layout_marginRight?設置組件的外邊界,類似我們搞網頁設計HTML/CSS中margin用法。
代碼舉例
activity_main.xml
<?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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RadioGroup android:id="@+id/orientation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp" > <RadioButton android:id="@+id/horizontal" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="水平" android:textSize="30dp" /> <RadioButton android:id="@+id/vertical" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="垂直" android:textSize="30dp" /> </RadioGroup> <RadioGroup android:id="@+id/gravity" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp" > <RadioButton android:id="@+id/left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="居左" android:textSize="30dp" /> <RadioButton android:id="@+id/center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="居中" android:textSize="30dp" /> <RadioButton android:id="@+id/right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="居右" android:textSize="30dp" /> </RadioGroup> </LinearLayout>
MainActivity.java
package com.example.android_demo02; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.widget.LinearLayout; import android.widget.RadioGroup; public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener { private RadioGroup orientation; private RadioGroup gravity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); orientation=(RadioGroup) findViewById(R.id.orientation); orientation.setOnCheckedChangeListener(this); gravity=(RadioGroup) findViewById(R.id.gravity); gravity.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i){ case R.id.horizontal: orientation.setOrientation(LinearLayout.HORIZONTAL); break; case R.id.vertical: orientation.setOrientation(LinearLayout.VERTICAL); break; case R.id.left: gravity.setGravity(Gravity.START); break; case R.id.center: gravity.setGravity(Gravity.CENTER_HORIZONTAL); break; case R.id.right: gravity.setGravity(Gravity.END); break; } } }
實現效果:
原文鏈接:https://blog.csdn.net/m0_56233309/article/details/123491720
相關推薦
- 2022-05-13 Python實現簡易的圖書管理系統_python
- 2024-03-17 Invoke-Expression : 無法將參數綁定到參數“Command”,因為該參數為空字符串
- 2022-06-21 C語言分別實現棧和隊列詳解流程_C 語言
- 2022-10-14 Sklearn中的二分類模型可以進行多分類的原理
- 2023-12-07 redis key
- 2022-09-27 Android開發EditText實現密碼顯示隱藏_Android
- 2023-03-19 Python學習之configparser模塊的使用詳解_python
- 2022-03-28 關于Qt添加opencv和libtorch庫的問題_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支