Android:无法根据需要正确设置 TableLayout [英] Android : Unable to set TableLayout properly as wanted

查看:23
本文介绍了Android:无法根据需要正确设置 TableLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在活动中显示来自数据库的数据.我想要一个表格和底部的按钮.对于数据,认为 TableLayout 将是它的最佳选择.我将 TableLayout 添加到 Horizo​​ntalView &到 ScrollView 使其垂直滚动 &水平.正在动态添加所有行 - 包括标题.这部分工作正常.

I am showing data from DB in an Activity. I want a Table and buttons on bottom. For data thought TableLayout would be best option for it. I added TableLayout to HorizontalView & to ScrollView making it scroll vertically & Horizontally. Am adding all rows dynamically - including header. This part is working fine.

我想要的是当内容小于屏幕宽度时,它应该还占据整个屏幕宽度.例如.如果桌子在纵向模式下很合适,那么当然在横向模式下,它们将在右侧留下空白空间.我不希望该空间被清空,而是被所有列占用.如果行宽大于屏幕宽度,则完全没有问题 - 出现水平滚动条.

I want is when the contents is less than the screen width, it should yet occupy the whole screen width. For eg. If a table fits well in Portrait mode then ofcourse for Landscape mode their will be blank space left on the right. I don't want that space to eb empty, instead to occupy by all columns. If the row width is greater than the screen width then no issues at all - as horiontal scrollbar appears.

我尝试了一些变体,但没有任何帮助.知道要进行哪些设置以利用所有空间(如果有的话)&展示下.

I tried few variations, but nothing helped out. Any idea what settings to make to utlize all space (if at all avbl) & show it.

是的,还有 1 个水平滚动条,它出现在最后一行.我想要它出现在最后一行下方 - 所以最后一行的边框是可见的.我的 XML :

And yes, 1 more issue of horizontal scrollbar, it appears just on the last row. I want it to appear below last row - so the border of last row is visible. My XML :

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:orientation="vertical">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:scrollbars="horizontal|vertical">

<HorizontalScrollView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:layout_marginBottom="10dp" > 

<TableLayout android:id="@+id/browseTable" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_marginBottom="20dp"
    android:background="#FF0000" android:stretchColumns="1,2,3">

</TableLayout>

</HorizontalScrollView>
</ScrollView>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content" 
android:orientation="horizontal"  android:gravity="center" 
android:layout_marginTop="15dp">
    <Button android:id="@+id/browseAddBtn" android:text="Add" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginRight="10dp" />
    <Button android:id="@+id/browseViewBtn" android:text="Edit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginRight="10dp"  />
    <Button android:id="@+id/browseReturnBtn" android:text="Return" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" />        
  </LinearLayout>

</LinearLayout>

输出:

推荐答案

我在解决您的问题时发现了一件事,关于 TableLayoutHorizo​​ntalScrollView 中使用时.当我们在水平滚动视图中使用表格布局时,android:stretchColumns 不起作用.因此,当屏幕旋转或列的宽度数据较少时,您会得到空白.

I found one thing while solving your problem, about TableLayout when used inside a HorizontalScrollView. The android:stretchColumns is not working when we use Table Layout inside Horizontal Scroll View. So that you are getting blank space when screen is rotated or columns have less width data.

我希望你明白这一点.我试图通过用其他一些替换水平滚动视图来解决这个问题.如果我得到解决方案,我会发布答案.再见.

I hope you understand this. I am trying to solve this problem by replacing Horizontal Scroll View with some other. I will post answer if i get solution. Bye.

编辑

Tvd 终于我找到了解决您问题的方法.对您的 XML 布局文件进行以下更改.

Hi Tvd finally I got the solution for your problem. make below changes to your XML layout file.

使用

        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:fillViewport="true"

如果要拉伸所有列,请在 TableLayout 中使用 stretchColumns = "*".

Use stretchColumns = "*" in TableLayout if you want all columns to be stretched.

再见.:-)

这篇关于Android:无法根据需要正确设置 TableLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆