简体   繁体   English

相对布局排列视图android

[英]Relative layout arrange views android

Friends, 朋友们

I am new to android so I could use your help. 我是android新手,因此可以使用您的帮助。 I want to implement a gallery where the user can flip between two images (presented in ImageView1 below). 我想实现一个画廊,用户可以在其中切换两个图像(在下面的ImageView1中呈现)。 The display that I want is given by: 我想要的显示方式为:

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

Where [TextView1] and [TextView2] are static texts that do not change with flipping between the two images. 其中[TextView1]和[TextView2]是静态文本,不会在两个图像之间切换而改变。 [ImageView1] is the actual image that is displayed and [ImageView2] is an image indicator (ie, image one is selected). [ImageView1]是显示的实际图像,[ImageView2]是图像指示符(即,选择了一个图像)。

I am using ViewPager to bound the images to [ImageView1], which is given below: 我正在使用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>

The main fragment where the images are shown: 显示图像的主要片段:

<?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>

The output from the code is: 该代码的输出为:

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

Where the image and page indicator have reversed positions. 图像和页面指示器的位置颠倒了。

Any ideas how I can fix this? 有什么想法我可以解决这个问题吗?

I do not want any vertical space among the views. 我不希望视图之间有任何垂直空间。 I have used a LinearLayout instead of the RelativeLayout for the main fragment, but that leaves space among the views depending on the screen size. 我为主要片段使用了LinearLayout而不是RelativeLayout ,但是根据屏幕大小,这些视图之间留有空间。

Try this way,hope this will help you to solve your problem. 尝试这种方式,希望这将帮助您解决问题。

<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>

Do This 做这个

<?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>

Change your code with this 以此更改您的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM