有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

不尊重XML中按钮的安卓换行符,但它使用Java代码

我有一个长文本的按钮,我想把它分成两行,按钮的宽度和其他按钮一样大

问题是,当我通过XML代码执行此操作时,AVD和我的硬件设备都会忽略换行符(但在编辑器预览中正确显示)。当我使用Java代码时,它受到尊重

这一点被忽略:

<Button
    安卓:id="@+id/button"
    安卓:layout_columnWeight="1"
    安卓:layout_gravity="fill"
    安卓:layout_rowWeight="1"
    安卓:onClick="playSpeech"
    安卓:tag="doyouspeakenglish"
    安卓:text="Do you\nspeak English?" />

这将正确应用换行符:

((Button) findViewById(R.id.button)).setText("Do you\nspeak English?");

XML方式被忽略的原因是什么?我还尝试了&#10;,正如这里所建议的:how can I display multiple lines of text on a button。同样,没有成功

完整布局代码

<?xml version="1.0" encoding="utf-8"?>
<安卓.support.constraint.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context=".MainActivity">

    <GridLayout
        安卓:layout_width="368dp"
        安卓:layout_height="495dp"
        安卓:layout_marginBottom="8dp"
        安卓:layout_marginEnd="8dp"
        安卓:layout_marginStart="8dp"
        安卓:layout_marginTop="8dp"
        安卓:columnCount="2"
        安卓:rowCount="4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            安卓:id="@+id/button"
            安卓:layout_columnWeight="1"
            安卓:layout_gravity="fill"
            安卓:layout_rowWeight="1"
            安卓:onClick="playSpeech"
            安卓:tag="doyouspeakenglish"
            安卓:text="Do you\nspeak English?" />

        <Button
            安卓:id="@+id/button2"
            安卓:layout_columnWeight="1"
            安卓:layout_gravity="fill"
            安卓:layout_rowWeight="1"
            安卓:onClick="playSpeech"
            安卓:tag="goodevening"
            安卓:text="Good Evening" />

        <Button
            安卓:id="@+id/button3"
            安卓:layout_columnWeight="1"
            安卓:layout_gravity="fill"
            安卓:layout_rowWeight="1"
            安卓:onClick="playSpeech"
            安卓:tag="hello"
            安卓:text="Hello" />

        <Button
            安卓:id="@+id/button4"
            安卓:layout_columnWeight="1"
            安卓:layout_gravity="fill"
            安卓:layout_rowWeight="1"
            安卓:onClick="playSpeech"
            安卓:tag="howareyou"
            安卓:text="How are you?" />

        <Button
            安卓:id="@+id/button5"
            安卓:layout_columnWeight="1"
            安卓:layout_gravity="fill"
            安卓:layout_rowWeight="1"
            安卓:onClick="playSpeech"
            安卓:tag="ilivein"
            安卓:text="I live in..." />

        <Button
            安卓:id="@+id/button6"
            安卓:layout_columnWeight="1"
            安卓:layout_gravity="fill"
            安卓:layout_rowWeight="1"
            安卓:onClick="playSpeech"
            安卓:tag="mynameis"
            安卓:text="My name is..." />

        <Button
            安卓:id="@+id/button7"
            安卓:layout_columnWeight="1"
            安卓:layout_gravity="fill"
            安卓:layout_rowWeight="1"
            安卓:onClick="playSpeech"
            安卓:tag="please"
            安卓:text="Please" />

        <Button
            安卓:id="@+id/button8"
            安卓:layout_columnWeight="1"
            安卓:layout_gravity="fill"
            安卓:layout_rowWeight="1"
            安卓:onClick="playSpeech"
            安卓:tag="welcome"
            安卓:text="Welcome" />
    </GridLayout>
</安卓.support.constraint.ConstraintLayout>

其他详情: 使用Android Studio 3.1.4和SDK v26

请注意,这个问题与How do I add a newline to a TextView in Android?不同,因为针对textview的建议解决方案不适用于我的button案例


共 (3) 个答案

  1. # 1 楼答案

    正如我在所有这些搜索之后所怀疑的,问题不在代码中。幸运的是,这不是一个损坏的安装,而只是一个不同的主布局,一直在emulator或my Phone中用作默认布局

    我有一个额外的activity_main.xml (v21),是我以前意外创建的,它包含旧代码。这就是为什么我对realactivity_main.xml的更新被忽略的原因

  2. # 2 楼答案

    这是一个我不知道的问题,因为我总是使用字符串资源
    而且这个问题只存在于设计中。在模拟器中,我可以看到换行符
    无论如何,在strings.xml:
    中添加这一行

    <string name="mytext">Do you\nspeak English?</string>
    

    并设置按钮的文本:

    android:text="@string/mytext" 
    

    这样\n就起作用了

  3. # 3 楼答案

    试试这个

          <LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical">
              <Button
                  android:id="@+id/button"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:onClick="playSpeech"
                  android:tag="doyouspeakenglish"
                  android:maxLines="2"
                  android:text="Do you \n speak English?" />
              <Button
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:onClick="playSpeech"
                  android:tag="doyouspeakenglish"
                  android:maxLines="2"
                  android:text="Do you \n speak English?" />
    
              <Button
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:onClick="playSpeech"
                  android:tag="doyouspeakenglish"
                  android:maxLines="2"
                  android:text="Do you \n speak English?" />
    
          </LinearLayout>