设置TextView文字居中,代码实现android:layout_gravity

本文详细介绍了如何在Android中使用XML和代码实现方式设置TextView的文字居中,并对比了android:gravity与android:layout_gravity的区别。同时,通过示例代码展示了如何在LinearLayout和RelativeLayout中调整组件的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设置TextView文字居中

android:gravity指的是控件的位置

而android:layout_gravity指的是这个layout的,是外面的

有2种方法可以设置TextView文字居中:
一:在xml文件设置:android:gravity="center"
二:在程序中设置:m_TxtTitle.setGravity(Gravity.CENTER);

备注:android:gravity和android:layout_gravity的区别在于前者对控件内部操作,后者是对整个控件操作。

例如:android:gravity="center"是对textView中文字居中
android:layout_gravity="center"是对textview控件在整个布局中居中
其实很容易理解,出现"layout"就是控件对整个布局的操作

 

设置TextView文字居中一般使用如下:

android:layout_width="fill_parent"              注意这里声明要为match_parent
android:layout_height="fill_parent"
android:gravity="center"

 

代码实现android:layout_gravity

通过查看SDK,发现有一个setGravity方法, 顾名思义, 这个应该就是用来设置Button组件中文字的对齐方式的方法了。
仔细找了一圈,没有发现setLayoutgravity方法,有点失望。 不过想想也对,如果这边有了这个方法,将Button放在不支持Layout_Gravity属性的Container中如何是好!

代码比较简单,但是发现它们还是花了我一点时间的

复制代码
Button button  = new Button(this);  
button.setText("One");  

//此处相当于布局文件中的Android:layout_gravity属性  
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
lp.gravity = Gravity.RIGHT;  
button.setLayoutParams(lp);  

//此处相当于布局文件中的Android:gravity属性  
button.setGravity(Gravity.CENTER);  
  
LinearLayout linear = new LinearLayout(this);  

//注意,对于LinearLayout布局来说,设置横向还是纵向是必须的!否则就看不到效果了。  
linear.setOrientation(LinearLayout.VERTICAL);  
linear.addView(button);  
setContentView(linear);   
复制代码

另外,要设置在RelativeLayout中的位置时使用addRule方法,如下:

params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
params.addRule(RelativeLayout.CENTER_IN_PARENT);  
mContainer.addView(progress,params);
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/dialog_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="@dimen/dimen_84dp" android:orientation="vertical"> <com.google.android.material.appbar.COUIDividerAppBarLayout android:id="@+id/appbar" style="@style/CommonAppBarStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/color_transparent" android:clickable="true" android:focusable="true" android:paddingLeft="@dimen/dimen_0dp" android:paddingRight="@dimen/dimen_0dp" app:elevation="@dimen/toolbar_elevation"> <com.coui.appcompat.toolbar.COUIToolbar android:id="@+id/toolbar" style="@style/COUIToolBarInAppBarLayoutStyle" android:layout_width="match_parent" android:background="@null" app:supportTitleTextAppearance="@style/textAppearanceSecondTitle" app:titleCenter="false" /> <com.coui.appcompat.tablayout.COUITabLayout android:id="@+id/tab_layout" android:background="@color/color_transparent" style="@style/COUISmallTabLayoutStyle" /> </com.google.android.material.appbar.COUIDividerAppBarLayout> <RelativeLayout android:id="@+id/content_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <com.filemanager.common.view.ViewPagerWrapperForPC android:id="@+id/view_pager_wrapper" android:layout_width="match_parent" android:layout_height="match_parent"> <com.filemanager.common.view.viewpager.RTLViewPager android:id="@+id/viewPager" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_below="@+id/appbar" android:clipChildren="false" android:clipToPadding="false" android:orientation="horizontal" /> </com.filemanager.common.view.ViewPagerWrapperForPC> </RelativeLayout> </LinearLayout> <!-- 其他布局保持不变... --> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="?attr/couiColorBackgroundElevatedWithCard" android:minHeight="@dimen/operation_btn_background_height"> <View android:id="@+id/button_divider" android:layout_width="match_parent" android:layout_height="@dimen/divider_background_height" android:layout_gravity="top" android:alpha="0" android:background="?attr/couiColorDivider" android:forceDarkAllowed="false" /> <!-- 修改后的底部操作栏布局 --> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 文本区域添加约束和最大宽度 --> <LinearLayout android:id="@+id/select_root_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="@dimen/dimen_16dp" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@id/btn_add_file" app:layout_constraintHorizontal_bias="0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintWidth_max="wrap" app:layout_constraintWidth_percent="0.7"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <!-- 添加最大行数和省略号处理 --> <TextView android:id="@+id/select_title_content" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:ellipsize="end" android:maxLines="2" android:text="@string/not_selected_file" android:textSize="16sp" android:lineHeight="22dp" android:textColor="?attr/couiColorLabelPrimary" android:textAppearance="@style/couiTextHeadlineXS" android:visibility="visible" tools:ignore="SpUsage" /> <ImageView android:id="@+id/select_arraw_up" android:layout_width="18dp" android:layout_height="18dp" android:src="@drawable/arrow_up" android:layout_marginLeft="@dimen/dimen_6dp" android:visibility="gone" android:layout_gravity="center"/> </LinearLayout> <!-- 添加最大行数和省略号处理 --> <TextView android:id="@+id/select_body_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:lineHeight="20dp" android:textSize="14sp" android:textAppearance="?attr/couiTextBodyXS" android:textColor="?attr/couiColorLabelSecondary" android:visibility="gone" android:ellipsize="end" android:maxLines="1" android:text="@string/selected_size"/> </LinearLayout> <!-- 按钮添加约束 --> <com.coui.appcompat.button.COUIButton android:id="@+id/btn_add_file" style="@style/Widget.COUI.Button.Large" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="@dimen/dimen_16dp" android:minWidth="@dimen/dimen_96dp" android:text="@string/label_add_recent_file" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </FrameLayout> </FrameLayout>这一版在文字长的时候会显示两行,能不能让文字居中显示在所在组件中
07-29
我说了只要相对分布不要擅自修改:<?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:orientation="vertical" android:padding="16dp" android:background="#F5F5F5"> <LinearLayout android:layout_width="396dp" android:layout_height="22dp" android:background="#E0E0E0" android:gravity="center_vertical" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:orientation="horizontal"> <ImageView android:layout_width="16dp" android:layout_height="16dp" android:contentDescription="Signal" android:src="@drawable/ic_signal" /> <ImageView android:layout_width="16dp" android:layout_height="16dp" android:layout_marginLeft="4dp" android:contentDescription="Battery" android:src="@drawable/ic_battery" /> </LinearLayout> </LinearLayout> <ImageView android:layout_width="48dp" android:layout_height="48dp" android:src="@drawable/Deco_App" android:contentDescription="Deco Logo" android:layout_gravity="center" android:layout_marginVertical="16dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to Deco" android:textSize="20sp" android:textStyle="bold" android:textColor="#666666" android:layout_gravity="center" android:layout_marginBottom="16dp"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I accept the Terms of Use and confirm that I have fully read and understood the Privacy Policy." android:textSize="12sp" android:textColor="#666666" android:buttonTint="#00C4CC" android:layout_marginBottom="8dp"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I confirm to join the User Experience Improvement Program. I understand that I can opt out of the program any time." android:textSize="14sp" android:textColor="#666666" android:buttonTint="#00C4CC" android:layout_marginBottom="16dp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Continue" android:textColor="#FFFFFF" android:background="@drawable/rounded_button" android:layout_gravity="center" android:textSize="16sp" android:layout_marginBottom="8dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Disagree and Quit" android:layout_gravity="center" android:textSize="14sp" android:textColor="#00C4CC" /> </LinearLayout>
最新发布
08-01
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.youth.banner.Banner android:id="@+id/main_banner" android:layout_width="match_parent" android:layout_height="200dp" android:layout_marginStart="10dp" android:layout_marginTop="10dp" android:layout_marginEnd="10dp" app:banner_loop_time="2000" app:banner_radius="15dp" /> <ImageView android:layout_width="190dp" android:layout_height="190dp" android:layout_marginStart="10dp" android:layout_marginTop="10dp" android:layout_marginEnd="10dp" android:src="@mipmap/picture_8" /> <View android:layout_width="match_parent" android:layout_height="0.5dp"/> <TextView android:layout_width="190dp" android:layout_height="30dp" android:layout_marginStart="10dp" android:layout_marginEnd="10dp" android:gravity="center" android:text="泰山" android:textColor="#333" android:textSize="16sp" /> <ImageView android:layout_width="190dp" android:layout_height="190dp" android:layout_marginStart="10dp" android:layout_marginTop="10dp" android:layout_marginEnd="10dp" android:src="@mipmap/picture_9"/> <View android:layout_width="match_parent" android:layout_height="0.5dp"/> <TextView android:layout_width="190dp" android:layout_height="30dp" android:layout_marginStart="10dp" android:layout_marginEnd="10dp" android:gravity="center" android:text="大小洞天" android:textColor="#333" android:textSize="16sp" /> <ImageView android:layout_width="190dp" android:layout_height="190dp" android:layout_gravity="right" android:layout_marginStart="10dp" android:layout_marginTop="10dp" android:layout_marginEnd="10dp" android:src="@mipmap/picture_9" /> <TextView android:layout_width="190dp" android:layout_height="30dp" android:layout_marginStart="10dp" android:layout_marginEnd="10dp" android:gravity="center" android:text="大小洞天" android:textColor="#333" android:textSize="16sp" /> </LinearLayout> 两张图片并排
06-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值