1

我有两个类似的问题,双方围绕涉及的ImageView推动TextView(S)和我想不出为什么。对于卡片列表,我希望图像的高度与其父母相匹配,而宽度适当地缩放,并且文字左对齐图像的右侧。对于弹出窗口,请使用下面的文本将宽度缩放到父级和缩放高度。避免Android在布局中将项目推出屏幕?

Current CardLayout output Current CardInfoLayout output

我很期待/寻找:

enter image description here enter image description here

当前card_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="120dp" > 

<android.support.v7.widget.CardView 
    android:id="@+id/card_view" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_margin="10dp" 
    android:layout_height="120dp" 
    card_view:cardCornerRadius="4dp" 
    card_view:elevation="14dp"> 

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

     <ImageView 
      android:id="@+id/image" 
      android:layout_height="match_parent" 
      android:layout_width="wrap_content" 
      android:scaleType="fitStart" 
      android:padding="4dp" 
      android:layout_gravity="left" 
      android:layout_weight="1"> 
     </ImageView> 

     <TextView 
      android:id="@+id/location" 
      android:textSize="16dip" 
      android:layout_toRightOf ="@+id/image" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:gravity="top" > 
     </TextView> 

     <TextView 
      android:id="@+id/abbreviation" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/location" 
      android:layout_toRightOf="@+id/image" 
      android:layout_toEndOf="@+id/image"> 
     </TextView> 

    </RelativeLayout> 
</android.support.v7.widget.CardView> 

</RelativeLayout> 

当前location_info_card.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ImageView 
    android:id="@+id/info_image" 
    android:layout_height="0dp" 
    android:layout_width="match_parent" 
    android:scaleType="fitStart" 
    android:padding="10dp" 
    android:layout_weight="1"> 
</ImageView> 

<TextView 
    android:id="@+id/info_abbreviation" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="20sp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:paddingBottom="4dp"> 
</TextView> 

<TextView 
    android:id="@+id/info_location" 
    android:textSize="16sp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:paddingBottom="4dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
</TextView> 
</LinearLayout> 

我从教程以及与单一和嵌套布局SO帖子尝试了几种解决方案,但似乎没有任何工作至今。这只是我目前所拥有的。

  1. 解决方案?
  2. 为什么文本被推到LinearLayout的底部而不是直接出现在底部?或者为什么它在和layout_toRightOfTextView上使用wrap_content时在RelativeLayout的屏幕上被推下?

关于#2,由于类似问题上的答案只针对特定的问题发布解决方案,没有任何解释,我似乎无法将其应用于我的情况。我正在寻找一种资源来理解布局和视图如何相互作用(单个和什么时候嵌套),如果解释太长的答案。

+0

您应该使用嵌套的布局和使用权重属性:) – HelloSadness

+0

对于弹出,请尝试在'ImageView'取出'layout_weight',其'layout_height'设置为'wrap_content'。 – clownba0t

+0

关于该卡,我在本地尝试了您的布局,并为我工作。只是为了确认,文本视图是否可见,使用白色以外的字体颜色,并且它们应该以编程方式正确显示文本? – clownba0t

回答

0

关于card_layout.xml布局,它看起来像你根本没有任何垂直对齐的最顶端TextView。您在那里有android:gravity参数,但它与android:layout_gravity不一样。它只设置视图的内容如何显示来看,它并不定位视图本身。你应该添加一行像android:layout_alignTop =“@ id/image”而不是重力。

至于第二个,你已经设置了卡的高度以匹配父母,所以没有什么奇怪的是它填满了整个屏幕。您需要将所有内容(布局和内部元素)的高度设置为wrap_content并删除重量属性,它们需要设置为所有视图以执行任何有用的操作。其他

一个重要的事情。我注意到你没有在你的ImageView上指定android:adjustViewBounds。您必须将其设置为true,否则视图将尝试设置与内在可绘制大小相关的比例,而不是比例以保持宽高比。

关于学习资料,我会先了解线性和相对布局的工作原理,首先,您的问题不一定与嵌套的东西有关。到目前为止,网络上有很多人,我相信你可以自己找到它们。在Android Studio中也不要害怕尝试。

+0

谢谢,虽然我还是不太明白。也许是一个更具体的问题:为什么当我有一个带有用于height&width和scaleType的wrap_content的ImageView时:fitStart嵌套在布局中,在某个点具有固定大小时,ImageView是否在它的右边有一个巨大的空间?我会希望它会将图像缩放到最大高度,然后获得图像+填充的最大宽度,而不是图像+填充+ ?? ......如果我明确设置了宽度,则它完全按照预期工作。 – Asmodean

+0

@Asmodean我刚刚注意到你也没有在你的视图中指定'android:adjustViewBounds',我已经更新了答案。希望有所帮助。 – Malcolm