0

我想创建一个与此类似的 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 设置为固定数字即可解决问题。但是,任何人都可以回答我的一些后续问题吗?

  1. 不是 0dp 使组件自动占用剩余空间。为什么在这种情况下它不起作用。

  2. 如示例所示,如何制作红色和绿色圆圈?

4

2 回答 2

0

试试这个(根据您的需要修改它):

使用线性布局:

<?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>
于 2013-10-30T08:53:44.783 回答
0

layout_width set do 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>

有关在此处使用 0dp 尺寸的更多信息。

于 2013-10-30T08:53:59.647 回答