【问题标题】:Setting a Layout to bottom of ScrollView?将布局设置为 ScrollView 的底部?
【发布时间】:2014-01-30 11:18:30
【问题描述】:

所以我有一个TableLayout,我想把它放在一个滚动视图中。但我有一个问题:

TableLayout 最初是空的,我通过点击一个按钮以编程方式将TableRows 添加到它。如果我正常地将TableLayout 放入ScrollView 中(即:当我滚动时,它可以正常工作),这工作正常。

但我想让 TableLayout 位于ScrollView 的底部。所以每次我添加一个TableRow,最后一个单元格总是会对齐到父ScrollView的底部。 (有点像移动聊天的工作方式 - 当您按下发送时,您的消息被放在底部,而所有其他消息都被向上推)。

如果我使用android:layout_gravity="bottom" 尝试实现这一点,我无法向上滚动,因此我无法看到任何被向上推到屏幕视图之外的行。但是,由于某种原因,我可以向下滚动(进入空虚状态),这是不可能的,因为最后一个 TableRow 应该在底部。

这是相关的 XML 代码(LinearLayout 可能是多余的/不需要的):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"    
android:background="@android:color/black"
android:orientation="vertical" >


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_gravity="center"
    android:layout_marginTop="0dp"
    android:layout_weight="0.1"
    android:orientation="horizontal" >

    <!-- 2 Buttons here -->
</LinearLayout>

<TextView
   <!-- attributes of textView> -->/>

<ScrollView
    android:id="@+id/tableScrollView"
    android:layout_width="match_parent"
    android:layout_weight="0.7"
    android:layout_height="0dip"
    android:scrollbars="none"
    android:orientation="vertical"
    android:background="@drawable/stripes_background" >

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">    

        <TableLayout
            android:id="@+id/outputTable"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:divider="@drawable/table_divider" 
            android:showDividers="middle">    
    </TableLayout>
    </RelativeLayout>


</ScrollView>

<Button
    <!-- Button attributes --> />

</LinearLayout>

分频器: 背景: 3

【问题讨论】:

  • 您可以使用RelativeLayout 代替LinearLayout,然后使用“alignparent_bottom”属性。希望对你有帮助

标签: java android xml scrollview tablelayout


【解决方案1】:

我试过了,效果很好。这是Button 的代码,只要我将新的TableRow 添加到TableLayout tblLayout,它就会滚动到底部。

btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                TableRow tbr = new TableRow(getApplicationContext());
                TextView tv= new TextView(getBaseContext());
                tv.setTextSize(80f);
                tv.setText("Item" + a++);
                tbr.addView(tv);
                tblLayout.addView(tbr);
                mScroll.post(new Runnable() { 
                    public void run() { 
                         mScroll.scrollTo(0, mScroll.getBottom());
                    } 
            });
            }
        });

它应该可以在任何放置按钮的地方工作。无论是在里面还是在外面。变量 a 是在顶部声明的 int。如果您没有得到任何东西并祝您编码愉快,请告诉我!

【讨论】:

  • 谢谢,但这并不能解决我的问题。我的问题与创建的整个 UI 有关,而不是它是否可以滚动到底部。我会在稍后发布屏幕截图以澄清
  • 很难说你想要什么我运行了我的代码,我认为它可以按照你的意愿工作。我想我可以解决你的问题。因此,如果您愿意,可以将代码发送给我
【解决方案2】:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_gravity="center"
        android:layout_marginTop="0dp"
        android:orientation="horizontal" >

        <!-- 2 Buttons here -->

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <TextView
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="uiop turn" />

    <ScrollView
        android:id="@+id/tableScrollView"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/botton_button"
        android:layout_below="@+id/textview"
        android:orientation="vertical"
        android:scrollbars="none" >

        <RelativeLayout
            android:id="@+id/relativelayout"
            android:layout_width="fill_parent"
            android:layout_gravity="bottom"
            android:layout_height="fill_parent" >

            <TableLayout
                android:id="@+id/outputTable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:showDividers="middle" >

            </TableLayout>
        </RelativeLayout>
    </ScrollView>

    <Button
        android:id="@+id/botton_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="roll" />

</RelativeLayout>

【讨论】:

  • 当我尝试这个时,我得到一个错误:android:layout_alignParentBottom,上面写着error: Error: Boolean types not allowed (at 'layout_alignBottom' with value 'true').
  • 不是“layout_alignBottom”,而是“layout_alignParentBottom”。
  • 哦,好吧,对不起。我改了,现在没有错误了。但我仍然有相同的结果:我可以向下滚动,但我不能向上滚动......而且没有任何向下滚动。
  • 更新了我的帖子
  • @Trust 添加您的可绘制对象和其他组件(如果适合您)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-07
相关资源
最近更新 更多