android layout空白,xml中的Android LinearLayout View项不会填充空白区域

我是android的新手,我有一个线性布局列表项,并希望TextView itemName填充行上剩余的空白区域,而其他两个视图(itemIcon和itemTimestamp)只占用他们需要的空间,因此使用了wrap_content .

我在ItemName android:layout_width上尝试了fill_parent和match_parent,但它最终隐藏了itemTimestamp视图.我尝试过RelativeLayout但没有运气,如果可能的话,我更愿意坚持使用Linearlayout.

感谢您的帮助.

android:id="@+id/itemNotificationBuzz"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#ffff0000"

>

android:id="@+id/notificationHeader"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal"

android:background="#ffff0000"

>

android:id="@+id/notificationIcon"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="left"

android:maxHeight="35dp"

android:maxWidth="35dp"

android:background="#FF000000"

android:padding="1dp"

android:src="@drawable/ic_action_place" />

android:id="@+id/notificationName"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:padding="5dp"

android:textSize="18sp"

android:textColor="@android:color/white"

android:background="#A4C739"

android:typeface="sans"

android:textStyle="bold"

android:maxLines="1"

android:text="Fname Lname"

/>

android:id="@+id/notificationTimestamp"

android:layout_width = "wrap_content"

android:layout_height="wrap_content"

android:gravity="right"

android:background="#FF000000"

android:ems="10"

android:maxLines="1"

android:text="2m"

android:textColor="@android:color/white"

android:typeface="sans" />

解决方法:

如果你想使用LinearLayout,你可以通过在LinearLayout和layout_weight中使用weightSum属性为你的项目Name TextView实现你想要的.

android:id="@+id/notificationHeader"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#ffff0000"

android:orientation="horizontal"

android:weightSum="1">

android:id="@+id/notificationIcon"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#FF000000"

android:gravity="left"

android:maxHeight="35dp"

android:maxWidth="35dp"

android:padding="1dp"

android:src="@drawable/ic_launcher"/>

android:id="@+id/notificationName"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:background="#A4C739"

android:gravity="center"

android:maxLines="1"

android:padding="5dp"

android:text="Fname Lname"

android:textColor="@android:color/white"

android:textSize="18sp"

android:textStyle="bold"

android:typeface="sans"

android:layout_weight="1"/>

android:id="@+id/notificationTimestamp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#FF000000"

android:gravity="right"

android:maxLines="1"

android:text="2m"

android:textColor="@android:color/white"

android:typeface="sans"/>

这将使您的项目名称填充所有空格,直到它到达最后一个元素.

注意:使用layout_weight时,必须将layout_width设置为0dp.而且你应该从你的最后一个元素中删除ems = 10,因为它会使你的notificationTimestamp元素具有更大的宽度,并且它会将Name元素向左推,留下一个空的空格.

标签:android,xml,android-layout

来源: https://codeday.me/bug/20190830/1766853.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个满足你要求的xml页面设计: ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 页面上方占用总页面的20% --> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.2" android:background="@android:color/holo_blue_light"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是页面上方的文本框" android:textSize="18sp" android:textColor="@android:color/white"/> </LinearLayout> <!-- 页面下方 --> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.8"> <!-- 左侧占比30% --> <LinearLayout android:orientation="vertical" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.3" android:background="@android:color/holo_green_light"> <!-- 使用RecycleViewAdapter --> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <!-- 填充4个button按钮 --> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮3"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮4"/> </LinearLayout> </LinearLayout> <!-- 右侧占比70% --> <FrameLayout android:id="@+id/frame_layout" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.7" android:background="@android:color/holo_orange_light"/> </LinearLayout> </LinearLayout> ``` 以上是一个简单的页面设计,其左侧使用了`RecyclerView`和`LinearLayout`,右侧使用了`FrameLayout`和4个不同的`Fragment`页面。你可以在java代码根据按钮的点击事件来切换不同的`Fragment`页面,同时也可以在`RecyclerView`填充不同的数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值