【问题标题】:RelativeLayout not showing items to the leftRelativeLayout 不显示左侧的项目
【发布时间】:2012-04-17 04:24:20
【问题描述】:

我有一个我认为很简单的布局。这包含一个滚动视图,其中包含一个水平滚动视图,因此我可以双向滚动。我已经简化了这个例子,所以它只有一个包含一个TextView 和另一个RelativeLayout 的RelativeLayout。这是 XML:

    <HorizontalScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fadeScrollbars="false"
        android:scrollbarAlwaysDrawHorizontalTrack="true" 
        android:fillViewport="true">
        <RelativeLayout
            android:id="@+id/mainLayout"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"                
            android:orientation="horizontal" >
            <TextView
                android:id="@+id/myText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"                    
                android:text="TEXT LABEL" />
            <RelativeLayout
                android:id="@+id/relativeLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/myText">
                     <Button android:id="@+id/button2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_toLeftOf="@+id/button1"
                        android:layout_above="@+id/button1"
                        android:text="2"/>
                    <Button android:id="@+id/button1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="1"/>
                     <Button android:id="@+id/button3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/button1"
                        android:text="3"/>                       
           </RelativeLayout>
        </RelativeLayout>
    </HorizontalScrollView>
</ScrollView>

我可以看到按钮 1 和 3,但我没有看到按钮 2。不幸的是,我需要这样做,因为页面是动态构建的(这个 xml 是我正在做的简化版本)而且我只知道我需要留下什么。我曾经让它工作,但不得不重新安排其他一些代码,它坏了。我错过了什么?

这是快照的链接:snapshot

【问题讨论】:

  • 你能发一个链接到它现在的样子吗?
  • 按钮2应该放在哪里?
  • button2 应该与 button1 的左上角对齐。我知道我可以将 button1 与 button2 的右下角对齐,但我需要以这种方式构建它。

标签: android android-relativelayout


【解决方案1】:

button2 在 button1 左上角的屏幕外。

我不确定您的预期结果是什么,但是您可以尝试将 button2 锚定到左上角(layout_alignParentTop、layout_alignParentLeft),然后根据您的需要在其右侧或下方显示 button1。

如果您只希望按钮 1 位于按钮 2 的右侧,这里是一个示例:

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2"
                android:layout_alignParentLeft="true" />

            <Button
                android:layout_toRightOf="@+id/button2"
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1" />

            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/button1"
                android:text="3" />
        </RelativeLayout>

取决于您实际尝试做什么(我对 button2 中的 android:layout_above="@+id/button1" AND android:layout_toLeftOf="@+id/button1" 感到困惑)

【讨论】:

  • 上面和toLeftOf都会把button2放在button1的左上角。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-02
  • 2011-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多