【问题标题】:TextView repositions when text is entered in EditText [XML]在 EditText [XML] 中输入文本时 TextView 重新定位
【发布时间】:2014-11-20 05:30:17
【问题描述】:

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

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>

【问题讨论】:

    标签: android xml layout move


    【解决方案1】:

    发生这种情况是因为您使用了TableLayout,但并未完全了解它的工作原理。它所做的是遍历并计算出它将有多少列。对于每个TableRow,它会将视图放在每一列中,从第一个定义的视图开始到最后一个。

    发生的情况是您的第一个 TextView 为艺术家姓名在第 1 列中。接下来的几个 TableRow 只有 1 列。然后您使用 setDateButton 访问TableRow,它创建了两列。现在,之前分配的所有其他视图都将在第 1 列中,在它们之后有第 2 列,如果它们有另一个视图,则与 editDate 一样宽。

    因此,setDateButton 现在与您的artistName TextView 一样宽,并且由于您使用的是android:drawableRight,因此图像被放置在Button 的右侧。

    您将需要调整 layout_columnlayout_span 值来调整布局。或者,我建议删除TableLayout 并适当地使用RelativeLayout

    【讨论】:

      【解决方案2】:

      尝试用LinearLayout 包裹ButtonTextView

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      相关资源
      最近更新 更多