在布局中正确设置对象

时间:2013-08-02 15:29:34

标签: java android android-layout

我有以下XML布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:background="@layout/bginset2" >
    <TextView
            android:id="@+id/tvTripName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Trip Name:"
            android:textStyle="bold"
            android:textColor="#2B00E4"
            android:layout_above="@+id/etShowLog"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:paddingLeft="2dip" />
        <EditText
            android:id="@+id/etFileName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/tvTripName"
            android:ems="10"
            android:inputType="textNoSuggestions"
            android:paddingLeft="2dip"
            android:singleLine="true"
            android:textColor="#999999"
            android:textSize="14dip"
            android:textStyle="normal" />
        <EditText
            android:id="@+id/etShowLog"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textMultiLine|textNoSuggestions"
            android:lines="20"
            android:textStyle="normal"
            android:paddingLeft="2dip"
            android:singleLine="false"
            android:textColor="#999999"
            android:textSize="14dip"
            android:gravity="top"
            android:layout_above="@+id/btnClear" />
        <Button
            android:id="@+id/btnClear"
            android:background="@drawable/otherbuttons"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Clear Trip"
            android:textStyle="bold"
            android:textColor="#FFFFFF"
            android:shadowColor="#000000"
            android:shadowDx="1"
            android:shadowDy="1"
            android:shadowRadius="2"
            android:layout_alignParentBottom="true" />
        <Button
            android:id="@+id/btnSave"
            android:background="@drawable/otherbuttons"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Save Trip"
            android:layout_gravity="right"
            android:textStyle="bold"
            android:textColor="#FFFFFF"
            android:shadowColor="#000000"
            android:shadowDx="1"
            android:shadowDy="1"
            android:shadowRadius="2"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true" />
</RelativeLayout>

它出现在我的手机上:

enter image description here

为什么Trip Name显得如此之低,为什么文字如此接近左边?

我该如何解决?

2 个答案:

答案 0 :(得分:2)

您的EditText

似乎存在冲突的属性
 <EditText
        android:id="@+id/etFileName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textMultiLine|textNoSuggestions"
        android:lines="22"
        android:textStyle="normal"
        android:paddingLeft="2dip"
        android:singleLine="false"
        android:textColor="#999999"
        android:textSize="14dip"
        android:layout_toLeftOf="@+id/tvTripName"  // here
        android:layout_alignParentRight="true"    // and here

您告诉它位于左侧对齐的TextView左侧,但也告诉它与父级对齐。

答案 1 :(得分:2)

    you can keep your cursor according to your choice by using this 
    EditText.setSelection(int index)
and for Trip name..just put Textview and edittext inside one layout
like

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tvTripName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="2dip"
            android:text="Trip Name:"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#2B00E4"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/etFileName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textNoSuggestions"
            android:paddingLeft="2dip"
            android:singleLine="true"
            android:textColor="#999999"
            android:textSize="14dip"
            android:textStyle="normal" />
    </LinearLayout>

    <EditText
        android:id="@+id/etShowLog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnClear"
        android:ems="10"
        android:gravity="top"
        android:inputType="textMultiLine|textNoSuggestions"
        android:lines="20"
        android:paddingLeft="2dip"
        android:singleLine="false"
        android:textColor="#999999"
        android:textSize="14dip"
        android:textStyle="normal" />

    <Button
        android:id="@+id/btnClear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/ic_launcher"
        android:shadowColor="#000000"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="2"
        android:text="Clear Trip"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btnSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_gravity="right"
        android:background="@drawable/ic_launcher"
        android:shadowColor="#000000"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="2"
        android:text="Save Trip"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />

</RelativeLayout>

Hope this will work for u..