2013-04-24 59 views
3

我想在屏幕上顯示只有兩個TextViews,一個在另一個之上。Android設備上查看:隱藏,如果layout_height太小

的TextView B:400dp高,在底部, TextView的一個:填充屏幕的其餘部分。但是,如果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> 

例子:
http://imageupper.com/i/?S0200010070011K1366827293817642
http://imageupper.com/i/?S0200010070021K1366827293817642

+1

的代碼,如果textviewb.getHeight <100然後textviewb.setVisibility(View.Gone)你可以做。 – 2013-04-24 14:58:00

+1

如何您的視圖B之前小於100dp,如果你想將它設置爲400dp並用視圖A填充剩餘的空間?可能是一個錯字?而且你將不得不d o一些檢查代碼來實現這個 – 2013-04-24 15:01:56

+0

我認爲你想'android:maxHeight =「400dp」'而不是'android:layout_height =「400dp」'因爲這會使你的TExtView總是400dp,所以你永遠不會少於100.然後以編程方式填充TextView,然後按照MarkoNiciforovic的建議。 – TronicZomB 2013-04-24 15:13:04

回答

0

如果你打算在其他幾個佈局要使用此功能,我會考慮建築它進入自定義視圖,您可以在其中添加額外的屬性customer:minSize = 100dp。在該自定義視圖,你很可能再添加馬爾科的建議(即textview.getHeight>客戶:minSize屬性然後textview.setVisibility(View.Gone /隱形)

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/

否則,我只是實現它在你的活動