【问题标题】:How do I fit the border to the LinearLayout如何使边框适合 LinearLayout
【发布时间】:2020-08-04 16:52:35
【问题描述】:

如何使边框完全适合其内容(通过 xml) 这是我的边界:

  <item>
        <shape android:shape="rectangle">
            <corners android:radius="10dp"/>
            <stroke android:color="#000000" android:width="2dp"/>
            <padding android:top="100dp"/>
        </shape>
    </item>

和我的布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="400dp"
    android:layout_height="150dp"
    android:orientation="horizontal"
    android:background="@drawable/border"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="6dp"
    android:gravity="center"
    android:paddingTop="25dp">

这是一张图片:

【问题讨论】:

    标签: java android xml border android-linearlayout


    【解决方案1】:

    看起来您正在尝试实现 RecyclerView 并且您的布局是元素的 ViewHolder.xml。

    我建议将 viewHolder 的宽度设置为match_parent,将高度设置为wrap_content,而不是指定固定的像素值来实现您想要的效果。

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@drawable/border"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="6dp"
        android:gravity="center"
        android:paddingTop="25dp">
    

    我会从您的 Border 资产中删除 padding 属性,而是建议通过 ViewHolders 布局管理间距。

    【讨论】:

    • 它仍然无法正常工作我按照你说的那样更改了 LinearLayout 并删除了填充,但它仍然保持不变
    • 你能添加一张图片看看有什么变化吗?
    • 别担心,我建议编辑您的帖子,包括您如何修复它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多