Android自定义布局重力

Android自定义布局重力,第1张

概述我正在尝试编写自己的布局类,将其放在常规网格中. 布局本身很好用,但我不能让这个布局中的按钮上的文本居中. 当我在LinearLayout中放置相同的按钮时,按钮文本按照需要居中,因此故障可能在我的布局中. 但是我的布局如何影响其子视图上文本的严重性?我怀疑它与布局参数有关,但我真的不知道它是如何工作的. 以下是一些可能与问题相关的代码段: 我的布局类WeightedGridLayout: pub 我正在尝试编写自己的布局类,将其放在常规网格中.
布局本身很好用,但我不能让这个布局中的按钮上的文本居中.
当我在linearLayout中放置相同的按钮时,按钮文本按照需要居中,因此故障可能在我的布局中.
但是我的布局如何影响其子视图上文本的严重性?我怀疑它与布局参数有关,但我真的不知道它是如何工作的.

以下是一些可能与问题相关的代码段:

我的布局类WeightedGrIDLayout:

public class WeightedGrIDLayout extends VIEwGroup {// ...@OverrIDeprotected voID onLayout(boolean changed,int left,int top,int right,int bottom) {    for (int i = 0,N = getChildCount(); i < N; i++) {        VIEw c = getChildAt(i);             // ...        // do some calculation        //        c.layout( childleft,childtop,childRight,childBottom );    }       }public static class LayoutParams extends VIEwGroup.marginLayoutParams  {    public position position = position( 0,0 );    public Span span = span( 1,1 );    // position and Span are local classes which are irrelevant herepublic LayoutParams( position position,Span span ) {    super(FILL_PARENT,FILL_PARENT);    this.position = position;    this.span = span;}public LayoutParams( position position ) {    this( position,span(1,1) );}public LayoutParams() {    this( position(0,0),1) );}public LayoutParams(marginLayoutParams params) {    super(params);}public LayoutParams(LayoutParams that) {    super(that);    this.position = that.position;    this.span = that.span;}public LayoutParams(Context context,AttributeSet attrs) {    super(context,attrs);}}

该类使用如下:

WeightedGrIDLayout grID = (WeightedGrIDLayout) findVIEwByID(R.ID.mainMenu);  LayoutInflater inflater = getLayoutInflater();  button button = (button)inflater.inflate(R.layout.buttonpropertIEs,null);  button.setText( "None" );  WeightedGrIDLayout.position pos = WeightedGrIDLayout.position(colIDx,rowIDx);  WeightedGrIDLayout.LayoutParams lp = new WeightedGrIDLayout.LayoutParams(pos);  lp.setmargins(5,20,5,20);           grID.addVIEw(button,lp );

以下是按钮的属性:

<button xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:background="@drawable/default_button"    androID:gravity="center"    androID:textSize="@dimen/text"    androID:textcolor="@color/text" ></button>

按钮文本显示在按钮顶部而不是中心.
为了将文本传到中心,我该怎么办?

解决方法 好的,我发现了问题:我无法覆盖onMeasure().只有onMeasure()在某个时刻调用函数measure()时,子视图的布局才有效.这是一个很好的工作示例 http://www.arpitonline.com/blog/2012/07/01/creating-custom-layouts-for-android/. 我只是希望自定义布局的官方文档会提到这一点. 总结

以上是内存溢出为你收集整理的Android自定义布局重力全部内容,希望文章能够帮你解决Android自定义布局重力所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1127330.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存