簡體   English   中英

我怎樣才能這樣一個android xml視圖

[英]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設置為固定數字可以解決該問題。 但是,有人可以回答我的一些后續問題嗎?

  1. 不是0dp會使組件自動占用剩余空間。 為什么在這種情況下不起作用。

  2. 如示例所示,如何制作紅色和綠色圓圈?

試試這個(根據您的需要修改):

使用線性布局:

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

有關在此處使用0dp大小的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM