![](/img/trans.png)
[英]How can I convert an XML to make a programmatically view in Android Java
[英]How Can I make a android xml view as this
我想创建一个与此类似的xml布局,但是我的xml代码无法解决。
这是我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#FFFFFFFF" >
<ImageButton
android:name = "@+id/switchOriginDest"
android:src="@drawable/switchorigdest"
android:layout_height = "fill_parent"
android:layout_width = "wrap_content"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/Origin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/rounded_edge"
android:padding="6dip"
android:hint="@string/mycurrentpos" />
<TextView
android:id="@+id/Destination"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/rounded_edge"
android:layout_below="@id/Origin"
android:hint="@string/desthint"
android:padding="6dip" />
</RelativeLayout>
我现在所拥有的是
----------更新------------
将TextView的layout_width设置为固定数字可以解决该问题。 但是,有人可以回答我的一些后续问题吗?
不是0dp会使组件自动占用剩余空间。 为什么在这种情况下不起作用。
如示例所示,如何制作红色和绿色圆圈?
试试这个(根据您的需要修改):
使用线性布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:weightSum="1"
android:orientation="horizontal" >
<ImageButton
android:name="@+id/switchOriginDest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/back_arrow" />
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="@+id/Origin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dip"
android:textSize="30sp"
android:hint="mycurrentpos" />
<TextView
android:id="@+id/Destination"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="desthint"
android:textSize="30sp"
android:padding="6dip" />
</LinearLayout>
</LinearLayout>
希望会有所帮助。
使用相对布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/switchOriginDest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="@drawable/back_arrow" />
<TextView
android:id="@+id/Origin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/switchOriginDest"
android:hint="mycurrentpos"
android:padding="6dip"
android:textSize="30sp" />
<TextView
android:id="@+id/Destination"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/Origin"
android:layout_toRightOf="@+id/switchOriginDest"
android:hint="desthint"
android:padding="6dip"
android:textSize="30sp" />
</RelativeLayout>
在您的示例中不清楚将layout_width设置为0dp。 如果使用权重来管理尺寸,则设置它是一个好习惯,但对于布局尺寸则不设置任何权重。
尝试使用wrap_content或一些固定大小,看看是否有帮助:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp" //here it seems ok, since you set the weight to 1
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#FFFFFFFF" >
<ImageButton
android:name = "@+id/switchOriginDest"
android:src="@drawable/switchorigdest"
android:layout_height = "fill_parent"
android:layout_width = "50dp" //since you src image is bigger than what you need to display, you can't rely on wrap content here ;)
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/Origin"
android:layout_width="200dp" //lets try with this size.
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/rounded_edge"
android:padding="6dip"
android:hint="@string/mycurrentpos" />
<TextView
android:id="@+id/Destination"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" //you probably want to align that on the bottom ?
android:background="@drawable/rounded_edge"
android:layout_below="@id/Origin"
android:hint="@string/desthint"
android:padding="6dip" />
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.