【问题标题】:android -- hide part of linear layout off-screen to the rightandroid - 将部分线性布局隐藏到右侧屏幕外
【发布时间】:2013-06-28 22:30:00
【问题描述】:

我在父线性布局中有两个线性布局。都是水平的。我希望所有l_child 显示和r_child 的一部分显示;而r_child 的其余部分将在屏幕外右侧。我该如何做到这一点?

<LinearLayout

    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/l_child"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/r_child"
        android:orientation="horizontal" >
    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 你能张贴你想要做什么的示意图吗?
  • 不确定我是否理解,但在这里:如果您愿意,每个子线性布局的图像都有三个图像视图;然后我想显示左孩子的所有三个图像,但只显示右孩子的第一个图像;而剩下的两个隐藏在屏幕外的右边。
  • 通过示意图我的意思是一个小图显示你想要完成的事情

标签: android android-layout android-linearlayout


【解决方案1】:

尝试给父 LinearLayout 一个负的右边距。

android:layout_marginRight="-50dp" 

其他解决方案可能是将父 LinearLayout 放在 ScrollView 中,并将子布局的宽度设置为所需的宽度。在这种情况下,您将能够将正确的布局滚动回屏幕。

【讨论】:

  • AFAIK 他们做到了。 stackoverflow.com/questions/10673503/…以防万一,我会很快检查的。
  • 有趣,我记得我之前尝试过但没有成功
  • 它确实有效。在具有 layout_marginLeft="-10dp" 的 LinearLayout 中使用 TextView 进行测试。见截图:dropbox.com/s/g5arw3zm780gtyp/…
  • 很好,我想知道它是否适用于较旧的 SDK 版本,我会在几年前尝试它。我仍然认为,在这种情况下,以编程方式将宽度设置为屏幕大小的百分比会在不同设备之间提供更高的一致性。
  • 当然。为什么不将两者结合起来呢?您可以动态设置负边距以扩展画布。参见示例:dropbox.com/s/qw1msodltt15jid/… 这是两个权重设置为 1 的布局,但父级的左边距为 -100dp。很酷的东西:) 以前没有探索过这些东西。
【解决方案2】:

布局权重仅用于分配布局以填充视图,因此不会溢出。

使用与密度无关的像素分配 layout_width 将导致不同尺寸设备的成功率不同。

因此,我相信您会更成功地根据屏幕尺寸以编程方式添加布局。

查看这篇文章获取屏幕宽度: Get screen dimensions in pixels

获得屏幕宽度后,您可以按像素分配内部布局尺寸(使用 LinearLayout.LayoutParams),以屏幕宽度的百分比计算。

如果您希望左侧布局占据屏幕的 80%,请使用 .8*screen_width 作为尺寸,如果您希望右侧布局然后溢出屏幕的 20%,请使用 .4*screen_width 作为那个尺寸。

【讨论】:

    【解决方案3】:

    你的意思是这样的

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_weight="1"
    android:orientation="horizontal"
    >
    
    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="70dp"
        android:layout_gravity="center_horizontal|top">
    
        <ImageView
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:id="@+id/imageView1"
            android:src="@drawable/ic_launcher"
            />
        <ImageView
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:id="@+id/imageView2"
            android:src="@drawable/ic_launcher"/>
        <ImageView
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:id="@+id/imageView3"
            android:src="@drawable/ic_launcher"/>
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="70dp"
        android:layout_gravity="center_horizontal|top">
    
    
        <ImageView
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:id="@+id/imageView1"
            android:layout_marginRight="200dp"
            android:src="@drawable/ic_launcher"/>
        <ImageView
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:id="@+id/imageView2"
            android:src="@drawable/ic_launcher"/>
        <ImageView
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:id="@+id/imageView3"
            android:src="@drawable/ic_launcher"/>
    </LinearLayout>
    </LinearLayout>
    

    【讨论】:

      【解决方案4】:

      您可以尝试使用 ViewPager,但我不知道您是否可以使用该 UI 组件获得您所描述的“出血画布”效果。

      编辑:也许您可以尝试使用图库小部件。

      这就是你所描述的吗?

      http://www.bangkokpost.com/media/content/20110915/309542.jpg

      【讨论】:

        猜你喜欢
        • 2019-01-20
        • 2016-10-06
        • 1970-01-01
        • 2016-01-29
        • 1970-01-01
        • 2012-03-07
        • 1970-01-01
        • 2013-10-02
        • 1970-01-01
        相关资源
        最近更新 更多