2011-07-12 68 views
0

我有以下布局(它很复杂,但我必须把它全部);我有3个LinearLayouts,以平铺的png作为背景水平放置:2必须放置在屏幕的顶部,最后放置在屏幕的底部。之间我必须把一个小部件画廊。LinearLayout和平铺背景的Android调整大小问题

我希望3个LinearLayouts获取用于其背景的png的高度(对于第一个而言是可以的,但对于调整大小的最后不是这样),并且该库采用屏幕的剩余大小。

我不明白为什么第三个LinearLayout被调整大小。

非常感谢您的帮助。我在代码后面放了2张图片以了解情况。

<?xml version="1.0" encoding="utf-8"?> 

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

<!-- THE FOLLOWING LAYOUT SHALL TAKE THE HEIGHT OF THE IMAGE USED FOR BACKGROUND 
=> IT IS OK --> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:background="@drawable/bg_title" 
     android:gravity="center_horizontal"> 

     <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/title" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/home_title" 
      android:textColor="@color/white" 
      android:gravity="center" 
      android:textSize="20sp"/> 
</LinearLayout> 

<!-- THE FOLLOWING LAYOUT SHALL TAKE THE HEIGHT OF THE IMAGE USED FOR BACKGROUND 
=> IT IS OK --> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:background="@drawable/bg_channel_title"> 
    <FrameLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/channels_frameLayout"> 

     <HorizontalScrollView 
      android:id="@+id/channel_scrollView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <LinearLayout 
       android:id="@+id/channel_layout" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="horizontal"> 

      </LinearLayout> 
     </HorizontalScrollView> 

    </FrameLayout> 
</LinearLayout> 

<!-- THE FOLLOWING LAYOUT SHALL TAKE THE REMAINING HEIGHT OF THE SCREEN --> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="vertical" 
    android:id="@+id/description_frameLayout"> 
    <Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/gallery" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
    /> 
</LinearLayout> 

<!-- THE FOLLOWING LAYOUT SHALL BE PLACED ON THE BOTTOM OF THE SCREEN 
AND TAKE THE HEIGHT OF THE IMAGE USED FOR BACKGROUND 
=> IT IS NOK, THE LAYOUT IS RESIZED IN HEIGHT --> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:background="@drawable/bg_description" 
    android:id="@+id/description_layout" 
    android:gravity="bottom"> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:gravity="left" 
     android:id="@+id/texts"> 

     <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="@color/orange"/> 

     <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/content" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="@color/white"/> 

    </LinearLayout> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:gravity="right" 
     android:id="@+id/btns"> 

     <ImageButton 
      android:id="@+id/info" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@null" 
      android:src="@drawable/btn_info" /> 

     <ImageButton 
      android:id="@+id/favorite" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@null" 
      android:src="@drawable/btn_favorite" /> 

    </LinearLayout> 
</LinearLayout> 

Good Layout that should be obtained

Bad layout that is currently obtained

回答

0

我终于决定继续前进,找到解决我的问题的其他方法。

现在所有的东西都是用xml配置完成的。

这是包含在屏幕底部的按钮布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:background="@drawable/bg_description" 
    android:id="@+id/description_layout" 
    > 

这里是bg_description.xml:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item> 
    <shape android:shape="rectangle"> 
     <solid android:color="#919191" /> 
    </shape> 
</item> 
<item android:top="1dp"> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
      <gradient 
       android:endColor="#525252" 
       android:startColor="#1B1B1B" 
       android:angle="90"/> 
    </shape> 
</item> 

一切都完美了。背景显示得很好,渲染效果比用png好,而且没有再调整大小的问题。

瓷砖的背景是好的,但对于简单的用例像这样的,我们要考虑通过xml配置更好,simpliest解决方案。

0

无处不在,你把 “机器人:layout_height =” 0dp “” 和你layout_weight = “1” 尝试: “机器人: layout_height =“wrap_content”“

此外,您肯定会错过包含文件末尾的图像按钮的布局中的android:layout_weight =“1”。

如果你的最后一个布局如此之大,那是因为你的@ drawable/bg_description肯定有点大,所以如果你不想在android:layout_weight =“1”放在imagebutton布局中,只需简单地减少你的可绘制高度。

+0

不幸的绘制具有正确的尺寸。我删除了所有其他布局,只保留包含按钮的布局。问题仍然存在,它调整大小。 – obo

+0

如果删除背景可绘制,它是否调整为按钮大小? – Khetzal

+0

我刚刚回答了我自己的问题,更准确地解释了我发现的问题和解决方案。 – obo

0

好的,我试着减少drawable的高度;有用。 我不明白为什么。 上面的第一张照片是来自Photoshop,我刚刚创建了一个工作区,其中包含手机的尺寸,并在其中构建了我的组件,尝试尊重一些宽高比。然后我拿起我的组件,而不修改它们的大小,并在我的应用程序中将它们用作drawable。我期待的宽高比也将在设备的屏幕上得到尊重。

它实际上受到两个第一个drawable(顶部)的尊重,但对于底部的一个我必须减少它...如果我这样做,它将在设备中工作,但在Photoshop它将不相关。

那么是什么?是不可能嘲笑我们的应用程序,并使用它们创建时的图形组件?对我来说这很奇怪。

+0

这种事最好的办法是使用九个补丁。你也可以制作3个drawable,并将它们放在drawable-hdpi,ldpi和mdpi中。不要忘记的是,设备的屏幕尺寸可以变化,320x240px之间1027 * XXX:/ – Khetzal

+0

正如我使用水平瓷砖的背景与一个像素的宽度的PNG在水平布局,是以屏幕的整个宽度,我不认为这个9补丁会对任何事情有所帮助。这里唯一我想了解的是:为什么一些在photoshop上渲染得很好的东西不在设备中? – obo

+0

我不确定你的问题。在我的应用程序,所有渲染上就像在故事板的应用;) – Khetzal