2016-10-11 97 views
0

我建了一个片段(与ConstraintLayout),它看起来像这样: Fragment image 我想重新使用它,例如加载它(添加到fragmentManager)到活动的“片段持有人”,但如果该持有人太小,图像将互相覆盖。 看到这个: Image problem 因此,图像不会调整大小...有没有解决方案来实现自动调整大小?拉伸ConstraintLayout及其内容

感谢, 佐尔坦

回答

0

将在自己的ConstraintLayout每个ImageViews的。 ImageViewslayout_widthlayout_height应该是wrap_content。其母公司ConstraintViewslayout_widthlayout_height应分别为match_parentwrap_content

锚左ConstraintView到顶部,并开始父,右ConstraintView使用layout_constraintTop_toTopOflayout_constraintStart_toStartOf,并且layout_constraintEnd_toEndOf顶部和结束。

同时使用layout_marginToplayout_marginLeft为左侧添加页边距ConstraintViews; layout_marginToplayout_marginRight的权利。

接下来,创建一个verticalGuideline作为您的片段的孩子。将其layout_constraintGuide_percent设置为0.5。给它@+id/guidelineid

设置左ConstraintView'slayout_constraintRight_toLeftOf@+id/guideline,右ConstraintView'slayout_constraintLeft_toRightOf@+id/guideline为好。

如果片段不是那么宽,这应该缩小ImageViews的大小。

如果你希望这是两个图像之间的最小间隙,你可以添加layout_marginRight向左ConstraintView,并layout_marginLeft向右ConstraintView。将每个设置为2dp将使最小差距为4dp

这里是一个布局文件示例。编辑容器ConstraintLayout'slayout_width看到它在行动:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.constraint.ConstraintLayout 
    android:layout_width="100dp" 
    android:layout_height="300dp" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintStart_toStartOf="parent" 
    android:background="#333333"> 

    <android.support.constraint.Guideline 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/guideline" 
     app:layout_constraintGuide_percent="0.5" 
     android:orientation="vertical"/> 

    <android.support.constraint.ConstraintLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintStart_toStartOf="parent" 
     android:layout_marginTop="16dp" 
     android:layout_marginLeft="16dp" 
     app:layout_constraintRight_toLeftOf="@+id/guideline" 
     android:layout_marginRight="2dp" 
     android:background="#FF0000"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      app:layout_constraintTop_toTopOf="parent" 
      app:layout_constraintStart_toStartOf="parent" 
      app:srcCompat="@mipmap/ic_launcher" 
      tools:layout_editor_absoluteY="0dp" 
      tools:layout_editor_absoluteX="0dp"/> 
    </android.support.constraint.ConstraintLayout> 

    <android.support.constraint.ConstraintLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintEnd_toEndOf="parent" 
     android:layout_marginTop="16dp" 
     android:layout_marginRight="16dp" 
     app:layout_constraintLeft_toRightOf="@+id/guideline" 
     android:layout_marginLeft="2dp" 
     android:background="#00FF00"> 


     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      app:layout_constraintTop_toTopOf="parent" 
      app:layout_constraintEnd_toEndOf="parent" 
      app:srcCompat="@mipmap/ic_launcher" 
      tools:layout_editor_absoluteY="0dp" 
      tools:layout_editor_absoluteX="0dp"/> 
    </android.support.constraint.ConstraintLayout> 
</android.support.constraint.ConstraintLayout> 

Enough Room Not Enough Room

+0

不错的办法! 我试图简化我的真实问题,所以我说我有两个图像,但我有四个...在一行中,我有4行...所以我有16个图像。 :) – Zoltan

+0

(突然发表我的评论...) 所以,你的解决方案看起来不错,但我不能用来解决我的真正问题... :( 更重要的是,我遇到了一个奇怪的Android Studio问题: 在文本模式我向我的图像写入'match_parent',然后点击设计,然后返回文本,Studio将它替换为固定值!:O为什么? – Zoltan