【问题标题】:Android View: hide if layout_height too smallAndroid View:如果 layout_height 太小则隐藏
【发布时间】:2013-04-18 04:41:26
【问题描述】:

我只想在屏幕上显示两个 TextView,一个在另一个之上。

TextView B:400dp 高,在底部, TextView A:填满屏幕的其余部分。

但是,如果 TextView A 的高度小于 100dp,则不应该显示(只有 TextView A 可见,其余只是空白)。

这只能通过 XML 来实现吗?

目前我正在使用这样的东西:

<?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:background="@color/white"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/blue"
        android:text="Image A"
        android:gravity="center"
        android:textSize="40sp" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="400dp"
        android:background="@color/green"
        android:text="Image B: 400dp"
        android:gravity="center"        
        android:textSize="40sp" />

</LinearLayout>

【问题讨论】:

  • 在代码中,如果 textviewb.getHeight
  • 如果您想将视图 B 设置为 400dp 并用视图 A 填充其余空间,您的视图 B 怎么能小于 100dp?可能是错字?你必须在代码中做一些检查才能实现这一点
  • 我认为你想要 android:maxHeight="400dp" 而不是 android:layout_height="400dp" 因为这会使你的 TEXtView 总是 400dp,所以你永远不会少于 100。然后以编程方式填充 TextView,然后按照 MarkoNiciforovic 的建议。
  • 我认为第二个 TextView 应该有“layout_weight="0"”。
  • @Alex Orlov:是的,这是一个错字,抱歉。我的意思是“如果 TextView A 的高度小于 100dp,则不应显示”。

标签: android android-layout android-linearlayout android-layout-weight


【解决方案1】:

如果您打算在其他几个布局中使用此功能,我会考虑将其构建到自定义视图中,您可以在其中添加额外的属性 customer:minSize=100dp。在该自定义视图中,您可能会添加 Marko 的建议(即 textview.getHeight > customer:minSize 然后 textview.setVisibility(View.Gone/Invisible)

http://developer.android.com/training/custom-views/index.html http://developer.android.com/guide/topics/ui/custom-components.html ... http://mobile.tutsplus.com/tutorials/android/android-sdk-creating-custom-views-2/

否则,我会在你的活动中实现它。

【讨论】:

    猜你喜欢
    • 2016-04-23
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多