【问题标题】:EditText cuts off textEditText 截断文本
【发布时间】:2014-09-06 13:47:15
【问题描述】:

我现在正在开发我的第一个应用程序,但遇到了一点 XML 问题。这就是我的屏幕目前的样子,你可以看到“投资”这个词被切成了两半。

这是产生此行为的代码:

<LinearLayout 
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="com.quinnmchugh.networthcalculator.MainActivity$PlaceholderFragment" 
android:orientation="vertical" >

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

    <TextView
        android:id="@+id/lblPrincipal"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/principal"
        android:layout_weight="2"
        android:gravity="right"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:textSize="16sp" />

    <EditText
        android:id="@+id/txtPrincipal"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:hint="ex. 5,000"
        android:layout_weight="3" />

</LinearLayout>

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

    <TextView
        android:id="@+id/lblStartingSalary"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/starting_salary"
        android:layout_weight="2"
        android:gravity="right"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:textSize="16sp" />

    <EditText
        android:id="@+id/txtStartingSalary"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:hint="ex. 60,000"
        android:layout_weight="3" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="24dp" >

    <TextView
        android:id="@+id/lblAnnualReturn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/annual_return"
        android:layout_weight="2"
        android:gravity="right"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:textSize="16sp" />

    <EditText
        android:id="@+id/txtAnnualReturn"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:hint="ex. 7%"
        android:layout_weight="3" />

</LinearLayout>

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

    <TextView
        android:id="@+id/lblPercentOfIncome"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/percent_of_income"
        android:layout_weight="2"
        android:gravity="right"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:textSize="16sp" />

    <EditText
        android:id="@+id/txtPercentOfIncome"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:hint="ex. 15%"
        android:layout_weight="3" />

</LinearLayout>

我需要解决什么问题才能让 EditText 完全包裹自己并显示字符串的全部内容?

【问题讨论】:

    标签: android xml android-edittext


    【解决方案1】:

    为被截断的元素添加一些填充。类似的东西:

    <TextView
        android:id="@+id/lblPercentOfIncome"
        ...
        android:paddingBottom="16dp" />
    

    【讨论】:

    • 所以这表明padding 没有被继承,您必须为每个对象重新声明它?
    • 它不是“继承的”,但您的父容器可以包含它自己的独立填充(可能会或不会被子视图使用)。
    【解决方案2】:

    在您的文本中添加一个新行“\n”。这将始终显示您的所有文本。

    【讨论】:

    • 这个解决方案虽然有效,但似乎是一个“便宜的黑客”。这是解决此问题的行业首选方法吗?
    • 哈哈我不确定行业首选。我只是一个工科学生。但它对我有用。
    • 'Hack' 的定义是由谁编写的(即,如果你编写了它,它就是一个 hack,如果我编写了它,它是一个快速的解决方法)。我不会这样做,因为 Android 会修剪文本前后的空格 - 特别是如果它是通过 Strings.xml 解析的。
    猜你喜欢
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多