2011-01-21 188 views
5

這在模擬器上工作正常,但不是在設備上,因爲我已經將高度硬編碼爲365.任何人都可以在這方面幫助我?如何在android中動態設置相對佈局的高度

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<TabHost android:id="@+id/tab_host" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <TabWidget android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:id="@android:id/tabs" 
     android:layout_gravity="bottom"/> 
     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" android:layout_height="fill_parent"> 
      <RelativeLayout android:id="@+id/first_tab" 
       android:orientation="vertical" android:layout_width="fill_parent" 
       android:layout_height="365px"> 
       <WebView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/webview" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/> 
        <ImageView 
         android:id="@+id/widget31" 
         android:layout_width="fill_parent" 
         android:layout_height="49px" 
         android:layout_x="0px" 
         android:layout_y="0px" 
         android:background="#39000000" 
         android:layout_alignParentBottom="true"> 
        </ImageView> 
        <ImageButton 
         android:id="@+id/btnFB" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:src="@drawable/fb" 
         android:background="#00000000" 
         android:layout_alignRight="@+id/widget31" 
         android:layout_alignParentBottom="true"> 
        </ImageButton> 
        <TextView 
         android:id="@+id/myTextView1" 
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content" 
         android:text="Share" 
         android:textColor="#111111" 
         android:textSize="14pt" 
         android:paddingBottom="5px" 
         android:layout_alignLeft="@+id/widget31" 
         android:layout_alignParentBottom="true"/> 
         <ImageButton 
          android:id="@+id/btnTwitter" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:src="@drawable/twitter" 
          android:visibility="invisible" 
          android:background="#00000000" 
          android:layout_alignRight="@+id/widget31" 
          android:layout_alignParentBottom="true"> 
         </ImageButton> 
      <!-- Replace TextView with your layout content for this tab --> 
      </RelativeLayout> 
      <LinearLayout android:id="@+id/second_tab" 
       android:orientation="vertical" android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
       <ScrollView 
        android:id="@+id/ScrollView01" 
        android:layout_height="365px" 
        android:layout_width="fill_parent"> 
        <WebView xmlns:android="http://schemas.android.com/apk/res/android" 
         android:id="@+id/webviewForRSS" 
         android:layout_width="fill_parent" 
         android:layout_height="365px" 
         android:background="#98012E"/> 
       </ScrollView> 
      <!-- Replace TextView with your layout content for this tab --> 
      </LinearLayout> 
      <LinearLayout android:id="@+id/edit_item_text_tab" 
       android:orientation="vertical" android:layout_width="fill_parent" 
       android:layout_height="fill_parent"/> 
      </FrameLayout> 
    </TabHost> 
</RelativeLayout> 

謝謝。

回答

15

您可以通過不同的方式動態地改變佈局高度-----

首先,

getLayoutParams().height= x; 
requestLayout(); or invalidate(); 

其次,

first_tab.setHeight(int pixels); 

使用的方式嘗試的人。 ......

6

您不應該使用px,而應該使用dp

android:layout_height="365dp" 

有一個讀here,要知道密度獨立像素(DP)更好。

使用dp單位來定義 應用程序的UI強烈推薦 ,以此確保對不同 屏幕 UI的正確顯示的方式。

相關問題