【问题标题】:How to put object in LinearLayout in a space left by other object?如何将LinearLayout中的对象放在其他对象留下的空间中?
【发布时间】:2013-07-12 19:05:34
【问题描述】:

我正在尝试构建布局。我使用水平线性布局,在其中我有一个图像视图、另一个布局和另一个图像视图。关键是两个图像视图应该在左侧和右侧,并且大小为 od 10 dp。中心 LinearLayout 应该被拉伸到屏幕但左右没有 10dp。

 <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/li_iv"
            android:layout_width="10dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="@drawable/pen" />

        <LinearLayout
            android:id="@+id/li_ll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:background="@drawable/back"
            android:orientation="vertical" >

            <TextView
                android:id="@android:id/title"
                android:layout_width="match_parent"
                android:layout_height="28dp"
                android:layout_marginLeft="28dp"
                android:layout_marginRight="28dp"
                android:textSize="22sp"
                android:textStyle="bold" />

            <TextView
                android:id="@android:id/content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:layout_marginLeft="29dp"
                android:layout_marginRight="31dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/back_repeat"
                android:text="aaaaaaaaaa\n"
                android:textSize="16dp" />
        <ImageView
            android:id="@+id/li_rl_center_right"
            android:layout_width="10dp"
            android:layout_alignParentRight="true"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:background="@drawable/pen" />
        </LinearLayout>

但这不会发生。右侧图像视图位于主 LinearLayout 的边界之外 - 在它的右侧,因此实际上在屏幕之外,因为主 LinearLayout 宽度填充了父级。 我不使用 relativeLayout,因为这样内容不会被拉伸到父级的大小,而是在 eclipse 中的图形布局中扩展到整个屏幕的大小,并且在手机上 imageViews 只有父级的默认大小。当 textview 内部有更多行时,侧面的图像不会拉伸。

有什么想法可以将这个 ImageView 放回布局的右侧吗?

【问题讨论】:

    标签: android xml android-linearlayout


    【解决方案1】:

    你应该使用相对布局,请试试这个代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    
        <ImageView
            android:id="@+id/li_iv"
            android:layout_width="10dp"
            android:layout_height="match_parent"
            android:layout_alignBottom="@+id/li_ll"
            android:layout_alignParentLeft="true"
            android:background="@drawable/pen" />
    
        <LinearLayout
            android:id="@+id/li_ll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/li_rl_center_right"
            android:layout_toRightOf="@+id/li_iv"
            android:background="@drawable/back"
            android:orientation="vertical" >
    
            <TextView
                android:id="@android:id/title"
                android:layout_width="match_parent"
                android:layout_height="28dp"
                android:layout_marginLeft="28dp"
                android:layout_marginRight="28dp"
                android:textSize="22sp"
                android:textStyle="bold" />
    
            <TextView
                android:id="@android:id/content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:layout_marginLeft="29dp"
                android:layout_marginRight="31dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/back_repeat"
                android:text="aaaaaaaaaa\n"
                android:textSize="16dp" />
        </LinearLayout>
    
        <ImageView
            android:id="@+id/li_rl_center_right"
            android:layout_width="10dp"
            android:layout_height="match_parent"
            android:layout_alignBottom="@+id/li_ll"
            android:layout_alignParentRight="true"
            android:layout_gravity="right"
            android:background="@drawable/pen" />
    
    </RelativeLayout>
    

    【讨论】:

    • 我不知道我对 Cote Mounyo 的回答是否让你感到困惑 - 我的意思是侧面图像不应该水平拉伸,这就是为什么它们需要 10dp,但它们应该垂直拉伸,具体取决于在 LinearLayout "@+id/li_ll" 大小上。
    • 当然,很清楚。试试我的提议。图像将垂直拉伸,因为它们的底层将与 LinearLayout 底层对齐。
    • 是的,它按我的意愿工作!我只重写了几行,我不得不错过一些东西,因为那些侧面图像没有拉伸。最后,我复制了所有内容,它可以正常工作!谢谢你,你救了我的神经,我很生气,因为这不能一直和我一起工作和玩耍!
    【解决方案2】:

    您需要将所有内容设置为android:layout_width="match_parent",然后使用weight 指定每个屏幕占用多少屏幕。

    【讨论】:

    • 抱歉,这不起作用,因为那样的话,图像会被拉伸,而这正是我不想要的。侧面图像应保持 10dp,因为如果它们被拉伸,平板电脑上的图像质量会很差。
    【解决方案3】:

    我使用一个隐藏的空间按钮。

    <LinearLayout
        android:id="@+id/buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="2dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp"
        android:orientation="horizontal">
    
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="Text"
            android:textSize="14sp"
            android:visibility="visible" />
    
        <!-- Non-Visible Button For Space: "Weight" and "Visibility" are important. -->
        <Button
            android:id="@+id/space"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Button"
            android:visibility="invisible" />
    
        <ImageButton
            android:id="@+id/other"
            style="@style/MiniPlayerButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:padding="10dp"
            app:srcCompat="@drawable/ic_other"
            android:textAllCaps="false"
            android:textSize="14sp"
            android:visibility="visible"
            android:contentDescription="Desc" />
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2013-02-18
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      相关资源
      最近更新 更多