【问题标题】:Relative layout arrange views android相对布局排列视图android
【发布时间】:2014-07-07 13:03:24
【问题描述】:

朋友们,

我是 android 新手,所以我可以使用您的帮助。我想实现一个用户可以在两个图像之间翻转的画廊(在下面的 ImageView1 中呈现)。我想要的显示由:

[TextView1]        [TextView2]    
           [ImageView1]
           [ImageView2]

其中 [TextView1] 和 [TextView2] 是静态文本,不会随着两个图像之间的翻转而改变。 [ImageView1] 是实际显示的图像,[ImageView2] 是图像指示符(即选择图像之一)。

我正在使用 ViewPager 将图像绑定到 [ImageView1],如下所示:

<?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="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <ImageView
        android:id="@+id/image_to_show"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="0dp"
        android:contentDescription="@string/image_description"
        android:scaleType="fitCenter" />

</RelativeLayout>

显示图像的主要片段:

<?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="wrap_content" >

    <TextView
        android:id="@+id/name_of_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:textColor="@color/card_text_color"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/date_taken"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:paddingBottom="10dp"
        android:textColor="@color/card_text_color"
        android:textSize="14sp" />

    <android.support.v4.view.ViewPager
        android:id="@+id/details_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />

    <LinearLayout
        android:id="@+id/page_indicator_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal"/>

</RelativeLayout>

代码的输出是:

[TextView1]        [TextView2]    
           [ImageView2]
           [ImageView1]

图像和页面指示器的位置相反。

有什么办法可以解决这个问题吗?

我不希望视图之间有任何垂直空间。我使用LinearLayout 而不是RelativeLayout 作为主要片段,但这会根据屏幕大小在视图之间留出空间。

【问题讨论】:

    标签: android android-layout android-fragments android-relativelayout


    【解决方案1】:

    试试这个方法,希望能帮助你解决问题。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1">
                <TextView
                    android:id="@+id/name_of_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/card_text_color"
                    android:textSize="14sp" />
            </LinearLayout>
    
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1">
                <TextView
                    android:id="@+id/date_taken"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/card_text_color"
                    android:textSize="14sp" />
            </LinearLayout>
    
        </LinearLayout>
        <android.support.v4.view.ViewPager
            android:id="@+id/details_image"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:paddingBottom="10dp"
            android:paddingTop="10dp" />
    
        <LinearLayout
            android:id="@+id/page_indicator_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="horizontal"/>
    
    </LinearLayout>
    

    【讨论】:

    • 它仍然在视图之间留下空间。 [ImageView2] 不再显示
    • ImageView2 是空白布局,因此未显示,因此请尝试在其中添加一些视图,然后将其显示在屏幕上。
    • 我在运行时将图像添加到布局中
    【解决方案2】:

    这样做

    <?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="match_parent" >
    
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="TextView1" />
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="TextView1" />
    </LinearLayout>
    
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    
    
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    
    </RelativeLayout>
    

    用这个改变你的代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多