Android记录:layout

本文详细解析了Android应用开发中常见的布局方式及属性设置,包括LinearLayout、RelativeLayout等的使用技巧,并通过实例展示了不同布局间的区别。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="10dp"
        android:text="@string/first"
        android:textSize="9dp"/>
     
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/second" />
   
    <LinearLayout
        android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical" >
     <TextView
         android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:text="@string/third"/>
     <Button
         android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:text="@string/third"/>
    </LinearLayout>
   
     <RelativeLayout
        android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical" >
     <TextView
         android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:layout_alignParentLeft="true"
      android:text="@string/third"/>
     <Button
         android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:layout_alignParentRight="true"
      android:text="@string/third"/>
    </RelativeLayout>
    
     <RelativeLayout  
       android:layout_width="wrap_content"  
    android:layout_height="wrap_content" android:gravity="right"
     android:padding="10dp">
         <TextView
         android:id="@+id/mTextView02"
         android:layout_alignParentLeft="true"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Case" />
     <Button
          android:id="@+id/mButton01"
          android:layout_alignParentRight="true"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="send" />
      </RelativeLayout>
      
</LinearLayout>

<!-- android:layout_width="fill_parent" 布局宽度塞满整个屏
  android:layout_width="wrap_content" 根据所在控件的宽度选择宽度-->

请用<!-- 支出类别选择图标组 --> <!-- 支出类别图标组 --> <LinearLayout android:id="@+id/expense_category_group_search_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="16dp"> <ImageButton android:id="@+id/category_food" android:layout_width="69dp" android:layout_height="wrap_content" app:srcCompat="@mipmap/hs_canyin_f" android:background="@color/button_default" /> <ImageButton android:id="@+id/category_shopping" android:layout_width="69dp" android:layout_height="wrap_content" android:background="@color/button_default" app:srcCompat="@mipmap/hs_gouwu_f" /> <ImageButton android:id="@+id/category_utilities" android:layout_width="69dp" android:layout_height="wrap_content" app:srcCompat="@mipmap/hs_shuidianfei_f" android:background="@color/button_default" /> <ImageButton android:id="@+id/category_phone" android:layout_width="69dp" android:layout_height="wrap_content" app:srcCompat="@mipmap/hs_tongxun_f" android:background="@color/button_default" /> <ImageButton android:id="@+id/category_entertainment" android:layout_width="69dp" android:layout_height="wrap_content" app:srcCompat="@mipmap/hs_yule_f" android:background="@color/button_default" /> </LinearLayout> <LinearLayout android:id="@+id/expense_category_group_search_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="16dp"> <ImageButton android:id="@+id/category_other_outcome" android:layout_width="69dp" android:layout_height="wrap_content" app:srcCompat="@mipmap/hs_qt_f" android:background="@color/button_default" /> </LinearLayout> <!-- 收入类别图标组 --> <LinearLayout android:id="@+id/income_category_group_search_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="16dp" android:visibility="gone"> <ImageButton android:id="@+id/category_salary" android:layout_width="69dp" android:layout_height="wrap_content" android:background="@color/button_default" app:srcCompat="@mipmap/hs_xinzi_f" /> <ImageButton android:id="@+id/category_collect_debt" android:layout_width="69dp" android:layout_height="wrap_content" android:background="@color/button_default" app:srcCompat="@mipmap/hs_shouzhai_f" /> <ImageButton android:id="@+id/category_bonus" android:layout_width="69dp" android:layout_height="wrap_content" android:background="@color/button_default" app:srcCompat="@mipmap/hs_jiangjin_f" /> <ImageButton android:id="@+id/category_investment" android:layout_width="69dp" android:layout_height="wrap_content" android:background="@color/button_default" app:srcCompat="@mipmap/hs_touzi_f" /> <ImageButton android:id="@+id/category_accident" android:layout_width="69dp" android:layout_height="wrap_content" android:background="@color/button_default" app:srcCompat="@mipmap/hs_yiwaisuode_f" /> </LinearLayout> <LinearLayout android:id="@+id/income_category_group_search_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="16dp" android:visibility="gone"> <ImageButton android:id="@+id/category_other_income" android:layout_width="69dp" android:layout_height="wrap_content" android:background="@color/button_default" app:srcCompat="@mipmap/hs_qita_f" /> </LinearLayout> 代替<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:padding="16dp"> <!-- 返回按钮 --> <Button android:id="@+id/back_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <!-- 标题 --> <TextView android:id="@+id/search_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询记录" android:textSize="24sp" android:textStyle="bold" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toStartOf="@id/back_button" /> <!-- 输入金额框 --> <EditText android:id="@+id/search_input" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="金额" android:inputType="numberDecimal" android:digits="0123456789." android:textSize="16sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toStartOf="@id/by_amount_button" app:layout_constraintTop_toBottomOf="@id/search_title" android:layout_marginTop="16dp"/> <!-- 按金额查找按钮 --> <Button android:id="@+id/by_amount_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按金额查找" app:layout_constraintStart_toEndOf="@id/search_input" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/back_button" android:layout_marginStart="8dp"/> <!-- 收入/支出按钮 --> <Button android:id="@+id/income_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="收入明细" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toStartOf="@id/expense_button" app:layout_constraintTop_toBottomOf="@id/by_amount_button" android:layout_marginTop="16dp"/> <Button android:id="@+id/expense_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="支出明细" app:layout_constraintStart_toEndOf="@id/income_button" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/by_amount_button" android:layout_marginTop="16dp"/> <!-- 日期选择按钮 --> <Button android:id="@+id/date_picker_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="选择日期" app:layout_constraintTop_toBottomOf="@id/income_button" android:layout_marginTop="16dp"/> <!-- 类别单选组:两行三列布局 --> <RadioGroup android:id="@+id/category_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="8dp" app:layout_constraintTop_toBottomOf="@id/date_picker_button" android:layout_marginTop="16dp"> <RadioButton android:id="@+id/category_food" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="餐饮" /> <RadioButton android:id="@+id/category_shopping" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="购物" /> <RadioButton android:id="@+id/category_utilities" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="水电" /> <RadioButton android:id="@+id/category_phone" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="话费" /> <RadioButton android:id="@+id/category_entertainment" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="娱乐" /> <RadioButton android:id="@+id/category_other" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="其他" /> </RadioGroup> <!-- 查询结果表格 --> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toBottomOf="@id/category_group" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginTop="16dp"> <TableLayout android:id="@+id/result_table" android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="*" /> </ScrollView> </androidx.constraintlayout.widget.ConstraintLayout> 的radiogroup部分
最新发布
06-28
你希望用以下的嵌套 `LinearLayout` + `ImageButton` 结构来替代原来的 `RadioGroup`,目的是实现一个更美观、图标化的类别选择组件。以下是完整的替换方案: --- ### ✅ 替换目标 用如下结构替代原 `RadioGroup`: - 使用两行水平排列的 `LinearLayout`,每个包含多个 `ImageButton`。 - 每个 `ImageButton` 代表一个类别图标。 - 实现“单选”逻辑(即点击一个按钮后,其他按钮恢复默认状态)。 --- ### ✅ XML 布局部分(已优化) ```xml <!-- 类别单选组:两行三列布局 --> <!-- 支出类别选择图标组 --> <!-- 支出类别图标组 --> <LinearLayout android:id="@+id/expense_category_group_search_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="16dp" app:layout_constraintTop_toBottomOf="@id/date_picker_button"> <ImageButton android:id="@+id/category_food" android:layout_width="0dp" android:layout_height="64dp" android:layout_weight="1" android:background="@color/button_default" android:scaleType="centerInside" app:srcCompat="@mipmap/hs_canyin_f" /> <ImageButton android:id="@+id/category_shopping" android:layout_width="0dp" android:layout_height="64dp" android:layout_weight="1" android:background="@color/button_default" android:scaleType="centerInside" app:srcCompat="@mipmap/hs_gouwu_f" /> <ImageButton android:id="@+id/category_utilities" android:layout_width="0dp" android:layout_height="64dp" android:layout_weight="1" android:background="@color/button_default" android:scaleType="centerInside" app:srcCompat="@mipmap/hs_shuidianfei_f" /> </LinearLayout> <LinearLayout android:id="@+id/expense_category_group_search_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="16dp"> <ImageButton android:id="@+id/category_phone" android:layout_width="0dp" android:layout_height="64dp" android:layout_weight="1" android:background="@color/button_default" android:scaleType="centerInside" app:srcCompat="@mipmap/hs_tongxun_f" /> <ImageButton android:id="@+id/category_entertainment" android:layout_width="0dp" android:layout_height="64dp" android:layout_weight="1" android:background="@color/button_default" android:scaleType="centerInside" app:srcCompat="@mipmap/hs_yule_f" /> <ImageButton android:id="@+id/category_other_outcome" android:layout_width="0dp" android:layout_height="64dp" android:layout_weight="1" android:background="@color/button_default" android:scaleType="centerInside" app:srcCompat="@mipmap/hs_qt_f" /> </LinearLayout> <!-- 收入类别图标组 --> <LinearLayout android:id="@+id/income_category_group_search_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="16dp" android:visibility="gone" app:layout_constraintTop_toBottomOf="@id/date_picker_button"> <ImageButton android:id="@+id/category_salary" android:layout_width="0dp" android:layout_height="64dp" android:layout_weight="1" android:background="@color/button_default" android:scaleType="centerInside" app:srcCompat="@mipmap/hs_xinzi_f" /> <ImageButton android:id="@+id/category_collect_debt" android:layout_width="0dp" android:layout_height="64dp" android:layout_weight="1" android:background="@color/button_default" android:scaleType="centerInside" app:srcCompat="@mipmap/hs_shouzhai_f" /> <ImageButton android:id="@+id/category_bonus" android:layout_width="0dp" android:layout_height="64dp" android:layout_weight="1" android:background="@color/button_default" android:scaleType="centerInside" app:srcCompat="@mipmap/hs_jiangjin_f" /> </LinearLayout> <LinearLayout android:id="@+id/income_category_group_search_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="16dp" android:visibility="gone"> <ImageButton android:id="@+id/category_investment" android:layout_width="0dp" android:layout_height="64dp" android:layout_weight="1" android:background="@color/button_default" android:scaleType="centerInside" app:srcCompat="@mipmap/hs_touzi_f" /> <ImageButton android:id="@+id/category_accident" android:layout_width="0dp" android:layout_height="64dp" android:layout_weight="1" android:background="@color/button_default" android:scaleType="centerInside" app:srcCompat="@mipmap/hs_yiwaisuode_f" /> <ImageButton android:id="@+id/category_other_income" android:layout_width="0dp" android:layout_height="64dp" android:layout_weight="1" android:background="@color/button_default" android:scaleType="centerInside" app:srcCompat="@mipmap/hs_qita_f" /> </LinearLayout> ``` --- ### ✅ Java/Kotlin 实现单选逻辑(以 Kotlin 为例) 在你的 `Activity` 或 `Fragment` 中添加如下代码来模拟单选行为: ```kotlin class SearchActivity : AppCompatActivity() { private var selectedButton: ImageButton? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_search) val buttons = listOf( findViewById<ImageButton>(R.id.category_food), findViewById<ImageButton>(R.id.category_shopping), findViewById<ImageButton>(R.id.category_utilities), findViewById<ImageButton>(R.id.category_phone), findViewById<ImageButton>(R.id.category_entertainment), findViewById<ImageButton>(R.id.category_other_outcome) ) buttons.forEach { button -> button.setOnClickListener { selectedButton?.setBackgroundResource(R.color.button_default) button.setBackgroundResource(R.color.button_selected) // 设置选中背景 selectedButton = button } } } } ``` > 你可以为选中的按钮设置不同的背景颜色或样式,比如使用 `R.drawable.selector_button` 来实现点击效果。 --- ### ✅ 补充建议 - **资源管理**:确保所有 `@mipmap/...` 图标都存在于 `res/mipmap/` 目录下。 - **动态切换收入/支出视图**:可以监听 `income_button` 和 `expense_button` 的点击事件,通过 `View.setVisibility(View.VISIBLE)` / `View.GONE` 动态切换显示哪一组类别图标。 - **图标尺寸统一**:建议统一图标大小和分辨率,避免视觉不一致问题。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值