【发布时间】: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