在EditText [XML]中输入文本时TextView重新定位

时间:2014-11-19 21:02:43

标签: android xml layout move

我有一个简单的表格布局,如下所示:

http://imgur.com/beU9LbR

我的问题是每当我在Artist,Venue或Comments EditText框中输入文本时,一旦文本字符串与Date TextView的开头垂直排成一行,TextView开始位置就会移动位置以保持与字符串一致人物;像这样:

http://imgur.com/7f7eal8

我不确定Button是否正在尝试将其终点与字符串匹配,或者TextView是否尝试将其起点与字符串匹配,但不管是哪种方式都不对。

有人能给我一些关于这里发生了什么的见解吗? 提前谢谢!

以下是显示页面的XML文件:

<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"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        style="@style/titleBar"
        android:id="@+id/addTitle"
        android:text="@string/add"          />

   <TableLayout
        style="@style/my_table"
        android:id="@+id/addTable"
        android:layout_below="@+id/addTitle">
        <TableRow>
            <TextView
                style="@style/largeText"
                android:text="@string/artistName"/>
        </TableRow>
        <TableRow>
            <EditText
                style="@style/tableEditText"
                android:id="@+id/editName"/>
        </TableRow>

        <TableRow>
            <TextView
                style="@style/largeText"
                android:text="@string/venue"/>
        </TableRow>
        <TableRow>
            <EditText
                style="@style/tableEditText"
                android:id="@+id/editVenue"/>
        </TableRow>

        <TableRow>
            <TextView
                style="@style/largeText"
                android:text="@string/date"/>
       </TableRow>
       <TableRow>
           <Button
               android:background="@android:color/transparent"
               android:drawableRight="@drawable/event_1"
               android:id="@+id/setDateButton"/>
           <TextView
               style="@style/tableEditText"
               android:layout_weight="3"
               android:layout_width="200dp"
               android:id="@+id/editDate"/>
       </TableRow>

       <TableRow>
            <TextView
                style="@style/largeText"
                android:text="@string/comments"/>
       </TableRow>
       <TableRow>
            <EditText
                style="@style/tableEditText"
                android:inputType="textNoSuggestions|textCapSentences|textMultiLine"
                android:lines="3"
                android:id="@+id/editComments"/>
        </TableRow>
    </TableLayout>

    <Button
        android:id="@+id/addButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/add"
        android:background="@android:color/transparent"
        android:textColor="@color/grey"
        android:padding="20dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

这是styles.xml文件:

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <style name="largeText">
        <item name="android:textColor">@color/grey</item>
        <item name="android:textSize">25sp</item>
        <item name="android:padding">3dp</item>
        <item name="android:layout_weight">1</item>
        <item name="android:gravity">left</item>
        <item name="android:layout_marginRight">20sp</item>
    </style>

    <style name="largeText2">
        <item name="android:textSize">25sp</item>
        <item name="android:padding">3dp</item>
        <item name="android:background">@color/bgOrange</item>
        <item name="android:textColor">@color/nearBlack</item>
        <item name="android:layout_weight">1</item>
        <item name="android:gravity">left</item>
        <item name="android:layout_marginRight">20sp</item>
    </style>

    <style name="titleBar">
        <item name="android:textColor">#fff</item>
        <item name="android:textSize">25sp</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_alignParentTop">true</item>
        <item name="android:layout_centerHorizontal">true</item>
        <item name="android:gravity">left</item>
        <item name="android:padding">20sp</item>
        <item name="android:background">@color/headingOrange</item>
    </style>

    <style name="my_table">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:padding">15sp</item>
    </style>

    <style name="tableEditText">
        <item name="android:inputType">textCapSentences|textNoSuggestions|textAutoComplete</item>
        <item name="android:background">@color/bgOrange</item>
        <item name="android:textColor">@color/nearBlack</item>
        <item name="android:gravity">left</item>
        <item name="android:padding">3dip</item>
        <item name="android:textSize">25sp</item>
        <item name="android:layout_weight">1</item>
    </style>

    <style name="smallIcon">
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_gravity">left</item>
        <item name="android:padding">2dp</item>
    </style>

</resources>

2 个答案:

答案 0 :(得分:0)

尝试使用Button

包裹TextViewLinearLayout

答案 1 :(得分:0)

这种情况正在发生,因为您使用TableLayout而未完全了解其工作原理。它的作用是通过并确定它将拥有多少列。对于每个TableRow,它会将视图放在从第一个定义视图到最后一个视图的每一列中。

发生的事情是,您对artistName的第一个TextView位于第1列。接下来的几个TableRow只有1列。然后使用setDateButton到TableRow创建两列。现在,之前分配的所有其他视图都位于第1列,第2列后面的第2列如果有另一个视图则会与editDate一样宽。

因此,setDateButton现在与您的artistName TextView一样宽,而且由于您使用的是android:drawableRight,因此图片会被放置在{{1}的右侧1}}。

您需要调整layout_columnlayout_span值来调整布局。或者,我建议您删除Button并正确使用TableLayout