[英]Android - swap views in LinearLayout via xml
Here is my xml code, it has an ImageView which stands before LinearLayout id = "linearLayout2" : 这是我的xml代码,它有一个ImageView,它位于LinearLayout id =“ linearLayout2”之前:
<?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:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1"
android:background="@drawable/border"
android:paddingBottom="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/ic_swap"
android:layout_gravity="center_vertical" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout2"
android:paddingBottom="5dp"
android:layout_marginRight="5dp">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fromStationField"
android:completionThreshold="1"
android:hint="From"
android:textAlignment="textStart"
android:gravity="left|start"
android:layout_gravity="left" />
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toStationField"
android:completionThreshold="1"
android:hint="To"
android:background="@android:color/transparent"
android:paddingRight="4dp"
android:paddingLeft="4dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
What I want to do is to make an ImageView stands after the LinearLayout id = "linearLayout2" like I show in an image bellow. 我想要做的是像在图像波纹管中所示的那样,在LinearLayout id =“ linearLayout2”之后使ImageView站立。
How to do this using only xml ? 仅使用xml如何执行此操作?
One solution could be inside of linearLayout1 to move the ImageView after linearLayout2. 一种解决方案可能是在linearLayout1内部,以将ImageView移到linearLayout2之后。 Then set linearLayout1's width to match_parent and the sibling views width to 0dp.
然后将linearLayout1的宽度设置为match_parent并将兄弟视图的宽度设置为0dp。 Lastly, use layout_weight to achieve the proportion of size you want for the ImageView and LinearLayout2.
最后,使用layout_weight获得ImageView和LinearLayout2所需的尺寸比例。
<?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:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:weightSum="10">
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="9"
android:orientation="vertical"
android:paddingBottom="5dp">
<AutoCompleteTextView
android:id="@+id/fromStationField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:completionThreshold="1"
android:gravity="left|start"
android:hint="From"
android:textAlignment="textStart" />
<AutoCompleteTextView
android:id="@+id/toStationField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:completionThreshold="1"
android:hint="To"
android:paddingLeft="4dp"
android:paddingRight="4dp" />
</LinearLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:src="@android:drawable/ic_swap" />
</LinearLayout>
</RelativeLayout>
In the layout below, I'm using negative margins in the ImageView
and a positive margin in the LinearLayout
container left of ImageView
so that we don't need to use layout_weight
property and can keep using match_parent
. 在下面的布局,我使用的负利润率
ImageView
在和切缘阳性LinearLayout
留下的容器ImageView
,这样我们就不需要使用layout_weight
财产,可以继续使用match_parent
。 For this you need to fix the size of your ImageView
. 为此,您需要固定
ImageView
的大小。 Check this code out: 检查此代码:
<?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:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1"
android:background="@drawable/border"
android:paddingBottom="5dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout2"
android:layout_marginRight="55dp"
android:paddingBottom="5dp">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fromStationField"
android:completionThreshold="1"
android:hint="From"
android:textAlignment="textStart"
android:gravity="left|start"
android:layout_gravity="left" />
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toStationField"
android:completionThreshold="1"
android:hint="To"
android:background="@android:color/transparent"
android:paddingRight="4dp"
android:paddingLeft="4dp" />
</LinearLayout>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/imageView"
android:src="@drawable/ic_swap"
android:layout_marginLeft="-55dp"
android:layout_gravity="center_vertical" />
</LinearLayout>
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.