【问题标题】:One child padding affects all child padding in Linearlayout一个子填充影响 Linearlayout 中的所有子填充
【发布时间】:2021-04-22 23:29:51
【问题描述】:

我在LinearLayout 有两个孩子,LinearLayout 使用ViewGroup.LayoutParams.MatchParent 作为LayoutParameter 的宽度和高度。两个孩子都是TextViewLayoutParameters = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WrapContent, parcentageOfChildWidth)。 我为第一个孩子 TextView 添加了 padding Top,但是这样做,第二个孩子也得到了 padding Top 并将内容向下推。

我想知道是否有一种方法可以将填充 Top 添加到第一个子元素并且其余子元素保持原样。 第一个文本视图包含素材图标,第二个包含普通文本。

【问题讨论】:

  • 您使用的是什么 layout_gravity for TextViews 和 Gravity for LinearLayout?

标签: android xamarin xamarin.android material-design android-linearlayout


【解决方案1】:

你可以这样做:

LinearLayout linearLayout = new LinearLayout(this);
linearLayout1.Orientation = Orientation.Horizontal;
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
linearLayout.LayoutParameters = layoutParams;
TextView textView1 = new TextView(this);
textView1.Text = "TextView1";
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
layoutParams1.TopMargin = 15;   //set the top padding         
textView1.LayoutParameters = layoutParams1;
TextView textView2 = new TextView(this);
textView2.Text = "ABC17405";
linearLayout.AddView(textView1);
linearLayout.AddView(textView2);

【讨论】:

  • 感谢您的回答。这很好用,但在我的情况下图标可能是不可见的,当它不可见时我也需要保留空间。
  • 如果你想让它在不可见时保持空间,你只需要设置textView1.Visibility = ViewStates.Invisible;
【解决方案2】:

感谢@Amin,让我朝着正确的方向前进! layout_gravity 和重力都是 TextViews 和 LinearLayout 的默认值。将材质图标的 TextView 的 layout_gravity 更改为 CenterHorizo​​ntal 使 padding top 可以在 LinearLayout 中工作。

var param = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WrapContent, cell.PercentageWidth); 
param.Gravity = GravityFlags.CenterHorizontal;
materialTextView.LayoutParameters = param;
materialTextView.SetPadding(0, 10, 0, 0);


                

【讨论】:

    猜你喜欢
    • 2021-08-30
    • 2013-06-23
    • 1970-01-01
    • 2020-08-31
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多