• Android UI 如何让TextView中的文字居中显示


    在我们平时做开发的过程中,总是会有布局要考虑,不光是大布局,像LinearLayout这样,小的如文字在View中的布局也影响到整体的美观,所以,在开发中要多多留意这些细节部分的布局,在Android中,文字居中很多人都不知道在哪里。包括很多Web开发人员也是,总以为是一个叫align的属性。我也不例外,不过借助百度搜索,我找到了解决方案,就把它存起来吧。以后可能又会忘记的。

    页面布局是LinearLayout里包一个TextView控件

    1.方法一:TextView设置android:gravity=”center”,同时TextView设置android:layout_width=”fill_parent”
    举例代码:

    <LinearLayout android:orientation=”horizontal”    
    android:layout_width=”fill_parent” android:layout_height=”40sp”    
    android:background=”@drawable/top_businesscard_1″>   
    <TextView android:layout_width=”fill_parent” android:gravity=”center”    
    android:textColor=”#447324″ android:textSize=”18dp” android:text=”@string/logo_text”    
    android:layout_height=”wrap_content” android:layout_marginTop=”10dp”/>   
    </LinearLayout>   

    方法二:LinearLayout设置android:gravity=”center_horizontal”,同时TextView设置android:layout_width=”wrap_content”
    举例代码:

    <LinearLayout android:orientation=“horizontal”   
    android:layout_width=“fill_parent” android:layout_height=“40sp”   
    android:background=“@drawable/top_businesscard_1″ android:gravity=“center_horizontal”>   
    <TextView android:layout_width=“wrap_content”   
    android:textColor=“#447324″ android:textSize=“18dp” android:text=“@string/logo_text”   
    android:layout_height=“wrap_content” android:layout_marginTop=“10dp”/>   
    </LinearLayout>   
  • 相关阅读:
    shardingjdbc分页查询从0开始的bug处理
    JDBC时间有12小时时差问题
    PHP基于xlswriter支持无限表头层级Excel导出
    超级简单!CentOS8 安装 MySQL 8.0,比喝水还简单
    服务器报错 Error: Failed to synchronize cache for repo 'AppStream'
    HttpReceiveRequestEntityBody 使用应注意的地方
    关于python中对象可变性的理解
    oracle导出数据
    vue 列表
    Linux安装Chrome浏览器
  • 原文地址:https://www.cnblogs.com/xuewater/p/2624048.html
Copyright © 2020-2023  润新知