为什么在layout_gravity中center_horizontal不生效, 而center却可以?

a382486075 2018-01-29 05:48:47
我的配置文件
<?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="wrap_content">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
/>
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"/>

</LinearLayout>

效果为
而把layout_gravity 改为center之后, 效果为
, 这个是为什么呢?
...全文
1180 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
LauJim 2018-02-28
  • 打赏
  • 举报
回复 1
好啦,午休的时候来解答, wrap_content 的含义是,包裹住内容, match_parent 的含义是,以“父类”为准。 比如一个网页吧,最外面有个大框,内容都在大框里,大框里边有个按钮,这个按钮是多宽,多高呢? wrap_content 就是以按钮内的文字为准,4个字的话,他就4个字宽,8个字就8个宽 match_parent 就是以他的上级为准,他的上级是大框,那么就大宽 你设置的是以内容为准,那么居中,自然就是原地不动了,得以大框为准,这样就可以居中了。 因为你设置的是文本,看不到框框,如果你设置按钮,就能看到按钮总的宽度了。
LauJim 2018-02-28
  • 打赏
  • 举报
回复 1
我即将学到这里了,现在我在《第一行代码》81页。 因为以前会做html静态网页,所以这一章看起来比较轻松。 wrap_content 这个含义,可以理解为包裹内容的最小值, 你设置的长高,都是最小值,那么居中就没有意义啦,因为内容已经被外边的框架紧紧地包围住了,那么它怎么有空隙可以上下居中呢? 你需要把高设置的“宽松”一点,才能垂直居中,对吧? 今天我计划学到100页,应该能彻底解答你的问题,别着急,我加油,晚上争取回答你的问题。
十二月的消亡 2018-02-27
  • 打赏
  • 举报
回复
因为你的layout_height是wrap_content,用match_parent就好了
YXTS122 2018-01-30
  • 打赏
  • 举报
回复 2
你没有设置LinearLayout的oriention啊!!!!
xuchao_blog 2018-01-30
  • 打赏
  • 举报
回复
我个人认为要满足你的需求是文字水平居中应该两个两个同事设置,即layout-gravity和gravity同时设置居中,一个是布局居中,一个是内容居中。
jklwan 2018-01-30
  • 打赏
  • 举报
回复
LinearLayout的orientation默认为水平(HORIZONTAL),这时候layout_gravity只有top,bottom,center_vertical生效,你设置为center_horizontal不会生效,而center是包含center_horizontal和center_vertical的,所以才有效。
前言项目里都会遇到几种页面,分别为加载、无网络、无数据、出错四种情况,经常要使用,所以封成库引用了,方便使用,顺便分享出来。先看一下效果: 原理比较简单,继承FrameLayout,在xml渲染完成后,加上加载、无网络、无数据、出错四个页面,根据需要控制显示哪一层,花了些时间,开了很多方法出来,支持很多属性的设置,算是比较实用,源码里已对各个方法的作用都加了注释,就不做过多解释了,项目GitHub地址:https://github.com/weavey/LoadingLayoutDemo,感兴趣的可以看看,欢迎指出问题。使用方式gradle引用:compile 'com.lai.weavey:loadinglayout:1.2'使用说明LoadingLayout支持全局配置,对所有使用到的地方都起效,需要在Application配置,如下: public class App extends Application {     @Override     public void onCreate() {         super.onCreate();         LoadingLayout.getConfig()                 .setErrorText("出错啦~请稍后重试!")                 .setEmptyText("抱歉,暂无数据")                 .setNoNetworkText("无网络连接,请检查您的网络···")                 .setErrorImage(R.mipmap.define_error)                 .setEmptyImage(R.mipmap.define_empty)                 .setNoNetworkImage(R.mipmap.define_nonetwork)                 .setAllTipTextColor(R.color.gray)                 .setAllTipTextSize(14)                 .setReloadButtonText("点我重试哦")                 .setReloadButtonTextSize(14)                 .setReloadButtonTextColor(R.color.gray)                 .setReloadButtonWidthAndHeight(150,40);     } }由于“加载”的页面,可能每个App都不一样,因此,LoadingLayout支持自定义LoadingPage,如下: LoadingLayout.getConfig()      .setLoadingPageLayout(R.layout.define_loading_page);同时,为了适应个别界面的“特殊需求”,LoadingLayout也支持局部设置各种属性,仅对当前对象生效,不影响全局。如下:        LoadingLayout  loading = (LoadingLayout) findViewById(R.id.loading_layout);         loading.setLoadingPage(R.layout.define_loading_page)                 .setEmptyText("暂无报告数据")                 .setErrorText("")                 .setNoNetworkText("")                 .setErrorImage(R.mipmap.ic_launcher)                 .setErrorTextSize(16)                 .setReloadButtonText("点我重新加载哦"); //等等为ReloadButton设置监听:loadingLayout.setOnReloadListener(new LoadingLayout.OnReloadListener() {             @Override             public void onReload(View v) {             }         });设置显示的页面: loadingLayout.setStatus(LoadingLayout.Loading);//加载  loadingLayout.setStatus(LoadingLayout.Empty);//无数据  loadingLayout.setStatus(LoadingLayout.Error);//错误  loadingLayout.setStatus(LoadingLayout.No_Network);//无网络  loadingLayout.setStatus(LoadingLayout.Success);//加载成功最后,在xml里面使用:Layout     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:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     app:isFirstVisible="true">     gravity="center"         android:textColor="@android:color/white"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:text="ContentView"/> Layout>注意: (1)isFirstVisible属性用来控制contentView一开始是否隐藏,由于LoadingLayout原理是在xml渲染完成后在contentView上铺上三层View,因此,一开始如果不隐藏,等contentView渲染完成后调用: loadingLayout.setStatus(LoadingLayout.Loading); 会造成界面闪烁的效果,影响体验,因此默认将contentView隐藏,所以数据加载完成后一定要调用loadingLayout.setStatus(LoadingLayout.Success);,将contentView显示出来。这样也能解决未获取到数据的情况下,被用户看到杂乱无章的布局,个人还是比较喜欢默认隐藏contentView; (2)为了方便管理,LoadingLayout只能有一个直属子View,类似ScrollView,添加两个直属子View会抛出异常throw new IllegalStateException("LoadingLayout can host only one direct child");; (3)由于AS会直接将自定义View的特性反应在预览界面,所以在使用LoadingLayout的时候,会无法看到被LoadingLayout包裹住的布局(默认为gone),因此也可以将isFirstVisible属性暂时设为true,预览布局。

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧