2014-02-16 37 views
0

我一直沒能找到一個類似的問題,我在StackOverflow上有一個 - 基本上,我有一個簡單的XML佈局,垂直LinerLayout使用layout_weight做出10/50/40分割,這是我想要的(如下圖所示):RelativeLayout不遵守父大小(與layout_weight)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"  
android:gravity="left" 
android:orientation="vertical"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_weight=".1"   
    android:text="@string/title" 
    android:textColor="@color/black" 
    android:textStyle="bold" /> 

     <LinearLayout  
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".5"  
     android:orientation="vertical" 
     android:background="@color/green"> 

    <HorizontalScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="top" 
     android:fillViewport="true"> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" /> 

    </HorizontalScrollView> 

    <ImageView 
     android:id="@+id/preview_bg" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/level_preview_back" 
     android:focusable="false" /> 

</LinearLayout> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_weight=".4"   
    android:text="@string/title" 
    android:textColor="@color/black" 
    android:textStyle="bold" /> 

</LinearLayout> 

但是 - 我想Horizo​​ntalScrollView和ImageView的將被覆蓋(即佔用上完全相同的空間所以我插入一個RelativeLayout這樣:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"  
android:gravity="left" 
android:orientation="vertical"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_weight=".1"   
    android:text="@string/title" 
    android:textColor="@color/black" 
    android:textStyle="bold" /> 

     <LinearLayout  
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".5"  
     android:orientation="vertical" 
     android:background="@color/green"> 

     <RelativeLayout     
      android:layout_width="match_parent"   
      android:layout_height="match_parent"> 

    <HorizontalScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="top" 
     android:fillViewport="true"> 

     <LinearLayout 
       android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" /> 

    </HorizontalScrollView> 

    <ImageView 
     android:id="@+id/preview_bg" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/level_preview_back" 
     android:focusable="false" /> 

    </RelativeLayout> 

</LinearLayout> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_weight=".4"   
    android:text="@string/title" 
    android:textColor="@color/black" 
    android:textStyle="bold" /> 

</LinearLayout> 

但是當我做到這一點,layout_weight(10/50/40分割)會突破,現在包含RelativeLayout的LinearLayout幾乎佔據了預覽中的所有屏幕。

任何人都遇到過這個問題或知道一個很好的解決方法?我也嘗試直接用RelativeLayout替換LinearLayout,並且遇到同樣的問題。

編輯:進一步探討這個問題,似乎更多的是它。正如我在下面評論的那樣,通過將LinearLayouts包裝到TextView中,我能夠解決這個問題,儘管我不確定爲什麼會這樣。更新我的SDK和插件沒有幫助(雖然我確實需要這樣做......)

隨着我的任務進一步移動,我遇到了第二個問題 - 我實現了第二個版本的重疊ScrollView & ImageView(這是用Karsten建議的FrameLayout完成的),但layout_weight問題重新開始。

測試各種安排,似乎這個問題與我使用的圖像非常相關,這個圖像的分辨率相當大。用較小的圖像替換它們可以糾正問題 - 它看起來像是在ImageView中使用大圖像,或者作爲LinearLayouts的背景往往會破壞整個屏幕的佈局。

編輯#2:我實際上已經將XML加載到兩個不同的設備上,並且它完全按照它的樣子呈現....這似乎是Eclipse XML佈局查看器的錯誤?

(請原諒可憐的XML格式 - Eclipse的Android的XML編輯器在這方面是令人沮喪的......)

回答

0

你已經發布的XML工作正常,我在Eclipse預覽。你正在運行Android插件的最新版本嗎?

注意,您可以用單一的FrameLayout更換中間的LinearLayout和RelativeLayout的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="left" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight=".1" 
     android:text="string/title" 
     android:textStyle="bold" /> 

    <FrameLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".5" 
     android:background="#00ff00" > 

     <HorizontalScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="top" 
      android:fillViewport="true" > 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" /> 
     </HorizontalScrollView> 

     <ImageView 
      android:id="@+id/preview_bg" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#ff0000" 
      android:focusable="false" /> 
    </FrameLayout> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight=".4" 
     android:text="string/title2" 
     android:textStyle="bold" /> 

</LinearLayout> 
+0

嗯..感謝您的答覆。我真的回到這裏來發布我自己的答案 - 看起來像是將TextViews包裝在自己的LinearLayouts中,並將layout_weight應用於LinearLayouts而不是直接應用於TextView,然後正確顯示。我也會檢查以確保我的Eclipse是最新的。 – Squimon

+0

另外,感謝您關於FrameLayout的提示 - 您說得對,他們似乎對我嘗試實現的種類更好。 – Squimon