【问题标题】:Write Multiline Text on Button in Android在Android中的按钮上写多行文本
【发布时间】:2013-11-11 01:27:45
【问题描述】:

我想知道,如何在按钮上写多行文字

 <Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="100dp"
    android:background="@drawable/layout_border"
    android:text="Hours   Mon - sat 5pm" />

我是这样的:-

但需要这种按钮:-

已编辑::--

现在我得到了类似的东西,通过使用@Sino K D 给出的答案:

但仍在寻求帮助,

在左侧添加drawable后,得到这个:-

【问题讨论】:

    标签: android xml android-widget android-button


    【解决方案1】:

    使用&amp;#10;

    (换行)

    例子:-

    android:text="Hi&#10;Hello"
    

    1) 在 ../res/values/strings.xml 中定义:

     <string name="multilines">Line1Line1\nLine2Line2</string>
    

    2) 在布局文件中引用:

     <Button
       android:id="@+id/btn_multilines"
       android:text="@string/multilines"
       android:layout_height="wrap_content"
       android:layout_width="fill_parent">
    </Button>
    

    【讨论】:

    • 小时是静态文本吗?
    • 那么我认为最好设计一个带有文本“小时”的图像并将其作为按钮的左可绘制图标。
    • 示例:- android:drawableLeft="@drawable/hour-text"
    • 小时也是文本而不是可绘制的
    • 正如我所说,您的回答太有用了,我使用了图片,但想知道如何在左侧添加文字,无论如何感谢您提供简单而出色的回答
    【解决方案2】:

    你为什么不通过编码来试试这个

    String styledText = "<small> <font color='#000000'>"
                + "Mon-Sat 5:00 pm" + "</font> </small>"+ "<br/>"
                + "<small> <font color='#000000'>" + "Closed on Sunday"
                + "</font> </small>";
    
        sendrequest.setText((Html
                .fromHtml(styledText)));
    

    【讨论】:

      【解决方案3】:

      使用&amp;#10;(换行)

      android:text="Hours&#10;Mon - sat 5pm"
      

      【讨论】:

        【解决方案4】:

        你可以用这个来实现。

        1->在布局中创建一个按钮

         <Button
                android:id="@+id/buton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Mon - Sat 5 pm\nClosed on sunday"
                />
        

        2-> 在你的项目中添加这个类。

        import android.graphics.Canvas;
        import android.graphics.Color;
        import android.graphics.ColorFilter;
        import android.graphics.Paint;
        import android.graphics.PixelFormat;
        import android.graphics.drawable.Drawable;
        
        public class TextDrawable extends Drawable {
        
            private final String text;
            private final Paint paint;
        
            public TextDrawable(String text) {
        
                this.text = text;
        
                this.paint = new Paint();
                paint.setColor(Color.WHITE);
                paint.setTextSize(20f);
                paint.setAntiAlias(true);
                paint.setFakeBoldText(true);
                paint.setShadowLayer(6f, 0, 0, Color.BLACK);
                paint.setStyle(Paint.Style.FILL);
                paint.setTextAlign(Paint.Align.LEFT);
            }
        
            @Override
            public void draw(Canvas canvas) {
                canvas.drawText(text, 0, 0, paint);
            }
        
            @Override
            public void setAlpha(int alpha) {
                paint.setAlpha(alpha);
            }
        
            @Override
            public void setColorFilter(ColorFilter cf) {
                paint.setColorFilter(cf);
            }
        
            @Override
            public int getOpacity() {
                return PixelFormat.TRANSLUCENT;
            }
        }
        

        3-> 在你的活动类中添加这些行

        Button button=(Button)findViewById(R.id.button);
        button.setCompoundDrawables( new TextDrawable("Hour"), null, null, null);
        

        【讨论】:

          【解决方案5】:

          对于 Android 布局,可以在元素中添加多行文本。

          在 res/values/strings.xml 中创建一个带有 endOfLine "\n" 字符的新变量。

          例如:

          <string name="multiplelines">Line1 \n Line2</string>
          

          在布局文件中引用它。例如,

          <Button
              android:id="@+id/start"
              android:text="@string/multiplelines"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent">
          </Button>
          

          【讨论】:

            【解决方案6】:

            您可以使用LinearLayout 代替按钮并达到类似的效果:

            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/layout_border"
            android:orientation="horizontal" >
                <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_content"
                android:textColor="@color/grey" // you'll have to define this yourself
                android:text="Hours"
                android:gravity="center_vertical" />
            
                <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Mon - Sat 5:00pm&#10;Closed on Sundays"
                android:gravity="center_vertical />
            </LinearLayout>
            

            这可能并不完美,但它是一个开始

            【讨论】:

              【解决方案7】:

              strings.xml中可以使用的解决方案,分隔符是\n在CDATA中

              <string name="switch_on_bluetooth"><![CDATA[Allumer\nBluetooth]]></string>
              

              【讨论】:

                【解决方案8】:

                添加

                <Button
                   android:layout_gravity="center_vertical" 
                />
                

                在您的 XML 文件中并使用“\n”在 java 文件中换行

                btn3.setText("Selected Orders\nOnly");
                

                https://i.stack.imgur.com/oXvNS.png

                【讨论】:

                  猜你喜欢
                  • 2020-06-22
                  • 2013-03-02
                  • 2014-09-12
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多