【问题标题】:Android multiple and dynamically images alignedAndroid 多张动态图片对齐
【发布时间】:2017-12-08 10:39:21
【问题描述】:

现在,我有一个 RecyclerView 和多个 CardView 来显示我的项目。

我的 CardView 有一个 RelativeLayout,我在其中添加了一些 ImageView(在本例中为 3 个,但可以更多)。

我的每个项目都会有一个图片列表来显示,假设#item1 有 Facebook、Twitter 和 Youtube,但#item2 只有 Facebook 和 Youtube 图片。

我应该如何对齐图像?如果我只是删除 Twitter 图片,我需要将我的 Facebook 图片的 layout_toLeftOf 更新为 Youtube 图片。

我认为我不应该对我所有的物品都做这种逻辑,它会超过权力......

示例图片: https://i.stack.imgur.com/uJZTi.png

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/card_height"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal">

            <ImageView
                android:id="@+id/coverImageView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:scaleType="centerCrop"
                />

            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="end|top">
                <ImageView android:background="@null"
                    android:id="@+id/facebook"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/facebook_icon"
                    android:layout_toLeftOf="@+id/twitter"/>
                <ImageView android:background="@null"
                    android:id="@+id/twitter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/twitter_icon"
                    android:layout_toLeftOf="@+id/youtube"/>
                <ImageView android:background="@null"
                    android:id="@+id/youtube"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/youtube_icon"
                    android:layout_alignParentRight="true"/>
            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="left|bottom"
                android:background="@android:drawable/screen_background_dark_transparent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/titleTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="16dp"
                    android:textSize="@dimen/text_size"
                    android:textColor="#FFFFFF"
                    android:textStyle="bold" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>

【问题讨论】:

    标签: android image layout


    【解决方案1】:

    你应该使用带水平方向的 LinearLayout !

    <LinearLayout
     ...
      android:orientation="horizontal">
    
        //ImageViews in priority order
    
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      在这种情况下最好使用 LinearLayout 并使用 layout_alignParentRight 和 layout_alignParentTop="true" 而不是 android:layout_gravity="end|top" ,如下所示:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:orientation="horizontal"
                      android:layout_alignParentRight="true"
                      android:layout_alignParentTop="true">
                      <ImageView android:background="@null"
                          android:id="@+id/facebook"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:src="@drawable/facebook_icon"
                          android:layout_weight="1"/>
                      <ImageView android:background="@null"
                          android:id="@+id/twitter"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:src="@drawable/twitter_icon"
                          android:layout_weight="1"/>
                      <ImageView android:background="@null"
                          android:id="@+id/youtube"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:src="@drawable/youtube_icon"
                          android:layout_weight="1"/>
                  </LinearLayout>
      

      【讨论】:

      • 嗯,看起来不错!我应该在 XML 上添加我想要的所有 ImageViews 并在 JAVA 中工作以删除我不想要的 ImageViews?因为有些项目没有所有的图像...
      • 您可以在代码中或在 xml 中静态设置可见性“消失”。 ( android:visibility="gone" ) 我真的没有看到你的问题
      • 我使用了 LinearLayout,但 android:layout_gravity="end|top" 不起作用。我保留了其他设置。泰
      • 请使用 layout_alignParentRight 和 layout_alignParentTop="true" 而不是 android:layout_gravity="end|top"..like 上面
      • 它实际上适用于 layout_gravity="end|top" 并且不适用于您的设置..
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多