2017-02-14 78 views
0

我正在AndroidStudio中执行一个Android项目,其中一个ScrollView包含一个垂直的LinearLayout,在该LinearLayout中有一个ImageView和另一个LinearLayout。我试图将ImageView设置为比LinearLayout小10倍,结果在预览窗口中显示正确,但在我的手机(带有API23的Xperia Z3)上不显示。XML布局权重不工作在android API23上的滚动视图

This is what I see in the preview window and what I want to achieve

但我的手机上的图片充满屏幕和图像高度的宽度是成正比的原始图像(几乎占满半个屏幕,所以远离它应该根据权重是什么)

该应用的工作原理与Google API Pixel 7.1.1 API17一样。

下面是代码的一部分:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:fillViewport="true" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <ImageView 
      android:layout_weight="1" 
      android:adjustViewBounds="true" 
      android:layout_height="0dp" 
      android:layout_gravity="center" 
      android:layout_width="wrap_content" 
      android:id="@+id/image" 
      android:scaleType="fitCenter" 
      android:src="@drawable/bjj2" /> 
     <LinearLayout 
      android:layout_weight="10" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="0dp"> 
      <LinearLayout 
       ...rest of the app 
     </LinearLayout> 
    </LinearLayout> 
</ScrollView> 

下面是完整的代码https://github.com/IvarsGoldingen/Bjjscorekeeper/blob/master/app/src/main/res/layout/activity_main.xml

有人能告诉我什么,我做错了。

回答

0

我们应该了解尺寸是如何计算的。 让我们一个一个来看看。

1)ScrollViewfill_parent/fill_parent(实际上是一个可以为fill_parent更改为match_parent/match_parent是据我记得过时,但它并没有在这种情况下重要)。 因此,ScrollView显示适合整个屏幕(宽度和高度)。

2)LinearLayout里面的ScrollViewmatch_parent/wrap_content。这是正确的。您应该使用wrap_content获取ScrollView以内的观看次数。 原因 - 是ScrollView用于包装视图与通常未知的高度(如列表)。想象这里match_parent作为高度 - 高度LinearLayout将根据滚动视图的高度来计算,这是整个屏幕的高度,并且屏幕高度可以高于屏幕高度。只是不合逻辑。但直到现在一切都还好。继续。

3)LinearLayout内的物品。您希望它们是父级布局高度的1/109/10。另外,您将内部项目的height设置为0,以表明您要从weights计算高度。 在这里你得到了一个周期的依赖关系。 LinearLayout高度为wrap_content并取决于内部元素的高度之和。 内部元素的高度由父母的高度计算。

此外,你想ImageViewLinearLayout高度的1/10。但实际上好像你想ImageView1/10屏幕的高度,因为LinearLayout高度可能大于屏幕的高度(并且看起来像这就是你的设备上所获得的)。

你说,在Android Studio预览中一切正常。 为了证明不是一切都OK试试这个:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:fillViewport="true" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <ImageView 
      android:layout_weight="1" 
      android:adjustViewBounds="true" 
      android:layout_height="0dp" 
      android:layout_gravity="center" 
      android:layout_width="wrap_content" 
      android:id="@+id/image" 
      android:scaleType="fitCenter" 
      android:src="@drawable/bjj2" /> 
     <LinearLayout 
      android:layout_weight="10" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="0dp"> 
      <View 
       android:layout_width="match_parent" 
       android:layout_height="1000dp"/> 

     </LinearLayout> 
    </LinearLayout> 

</ScrollView> 

只是把一些大(比屏幕的高度越大)看你的内内LinearLayout内,你会看到,在预览图像比的1/10更大的屏幕的高度。但它是LinearLayout1/10的高度。

如何解决您的问题? 我认为唯一的方法是计算屏幕的高度1/10和你想拥有和位图设置为ImageView图像的大小调整位图。 或任何其他方式设置精确的高度为ImageView

或审查你的布局,创造不同的东西。

+0

我也希望图像是的LinearLayout的,而不是屏幕的1/10 - 没有必要作为最终的应用程序,但只是作为一个例子,这是不工作我的手机上。我对你的代码进行了实验,只有当内部项目的高度超过屏幕高度(应用程序变成可滚动)时,问题才存在。 这里我设置视图200dp的高度和图像是布局的1/10: http://i.imgur.com/YHZvFMO.png 这里查看是400dp,因为你可以看到图像填充屏幕的宽度,并且是不布局的1/10: http://i.imgur.com/HP4cPUH.png http://i.imgur.com/9ybTt3z.png –

+1

我看到的依赖性问题我的代码。请告诉我,如果我正确理解这一点:“我不应该一起使用LinearLayout上的layout_weight和wrap_content,因为它们将相互依赖”。但是这种问题只有在scrollview变成可滚动时才会出现(内部元素超过屏幕高度)。 –

+0

您是否设法解决您的问题? – MadNeox