【问题标题】:In Android, I have a view that disappears from screen if the screen height isn't big enough在 Android 中,如果屏幕高度不够大,我的视图会从屏幕上消失
【发布时间】:2014-04-15 12:39:02
【问题描述】:

我有一个带有标题、列表视图和带有几个按钮的底部栏的垂直布局。如果屏幕足够高以容纳所有数据,则一切正常,但是一旦 listView 中有太多数据无法容纳在屏幕上,带有按钮的底部工具栏就会消失。如果我将手机从纵向旋转到横向,也会发生同样的事情(因为数据适合纵向屏幕而不是横向屏幕)。这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_list" 
    android:orientation="vertical"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    style="?popupBgStyle" >

    <TextView android:id="@+id/popup_lbl"       
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        style="?popupHeaderStyle" />

    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"    
        style="?insideBgStyle" >

        <TextView android:id="@+id/chk_list_msg" 
            android:visibility="gone"
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent" />

        <ListView android:id="@+id/chk_list_view" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout android:id="@+id/footer"
        android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_margin="0dip"
        android:padding="0dip"
        style="?popupFooterStyle" >

        <Button android:id="@+id/btn_popup"  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            style="?popupBtnStyle" />
    </LinearLayout>
</LinearLayout>

我尝试将 layout_weight=1 设置为底部工具栏,并将 layout_weight=0 设置为 listView 的布局,但这无济于事。有什么建议吗?

【问题讨论】:

  • 为什么不把它放在滚动视图中呢?
  • 我可以看到页脚的可见性设置为消失。您可以尝试使用相对布局而不是线性布局吗?
  • 可见性在运行时切换,我已将其从 OP 中删除。是的,RelativeLayout 也有同样的问题

标签: android layout view


【解决方案1】:

ListViews 的高度设置为 0dp,并将权重设置为 1。这样它会一直拉伸,但顶部和底部会保留在屏幕中。

【讨论】:

  • 你的意思是说ListView,还是持有ListView的中间布局?
  • 你是对的,我的意思是包含ListView的中间布局。将ListViews 的高度设置为match_parent
【解决方案2】:

我已对此进行了检查并用于测试目的。我修改了一些值 试试这个并根据您的需要进行修改

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="10" >

<TextView
    android:id="@+id/popup_lbl"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:text="dummy"
    android:textColor="#000000" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="6"
    android:orientation="vertical"
    android:weightSum="2" >

    <TextView
        android:id="@+id/chk_list_msg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="dummy"
        android:textColor="#000000" />

    <ListView
        android:id="@+id/chk_list_view"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" />
</LinearLayout>

<LinearLayout
    android:id="@+id/footer"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_margin="0dip"
    android:layout_weight="6"
    android:orientation="horizontal"
    android:padding="0dip" >

    <Button
        android:id="@+id/btn_popup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:text="dummy"
        android:textColor="#000000" />
</LinearLayout>

希望对你有帮助

【讨论】:

  • 在您的示例中,weightsum=10,但 3 个权重的总和为 14 (2 + 6 + 6)。你有这样做的目的吗?
  • 哦...对于底部的线性布局,将其设为 2。
  • 我已经测试过了……我这边没问题……我在这里捕获了这个布局图形视图……我不知道如何在这里发布这就是我等待的原因。 .
  • 您的代码不好——您将 ListView 高度设置为“380dp”,但它需要是 wrap_content
  • 我已经编辑了 GVSharma 的源代码。这将帮助您在权重总和的帮助下找到答案...试试看...
猜你喜欢
  • 2013-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-08
  • 2013-01-02
  • 1970-01-01
  • 1970-01-01
  • 2017-12-07
相关资源
最近更新 更多