gravity 经典案例分析

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/rec_white_conner"
android:orientation="vertical">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#333"
android:layout_marginTop="20dp"
android:text="提示"
android:visibility="visible"/>

<FrameLayout
android:id="@+id/contentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="35dp"
android:paddingBottom="35dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:background="@drawable/rec_white_conner"
android:orientation="vertical">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:textColor="#333"
android:gravity="left"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:lineSpacingExtra="8dp"
android:text="hello"/>
</FrameLayout>

<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#999"/>

<LinearLayout
android:id="@+id/dialog_bottom"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="horizontal">

<Button
android:id="@+id/negativeButton"
android:textColor="#cccccc"
android:layout_width="0dp"
android:layout_weight="1"
android:background="@drawable/sel_dialog_btn_conner_l_b"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/tips_cancel"/>
<View
android:id="@+id/dialog_line"
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#999"/>

<Button
android:id="@+id/positiveButton"
android:textColor="#0070ee"
android:background="@drawable/sel_dialog_btn_conner_r_b"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/tips_sure"/>
</LinearLayout>
</LinearLayout>


注:如图所示 想让message 字数不超过一屏时,居中显示,超过一屏时居左显示

知识点:android:layout_gravity和android:gravity属性的区别

gravity的中文意思就是”重心“,就是表示view横向和纵向的停靠位置

 android:gravity:是对view控件本身来说的,是用来设置view本身的文本应该显示在view的什么位置,默认值是左侧

android:layout_gravity:是相对于包含改元素的父元素来说的,设置该元素在父元素的什么位置

 

比如TextView: android:layout_gravity表示TextView在界面上的位置,android:gravity表示TextView文本在TextView的什么位置,默认值是左侧


1.如图message的长宽均是 wrap_content 自适应的
2.
android:gravity="left" android:layout_gravity="center"
这两个属性表示TextView在界面上的位置, 但是字体是居左的,

这样1.+2.就会达到想要效果~~
 
 


转载于:https://www.cnblogs.com/jovita21/p/4587952.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值