Android 一个子填充会影响Linearlayout中的所有子填充
我在Android 一个子填充会影响Linearlayout中的所有子填充,android,xamarin,xamarin.android,material-design,android-linearlayout,Android,Xamarin,Xamarin.android,Material Design,Android Linearlayout,我在LinearLayout中有两个子项,LinearLayout使用ViewGroup.LayoutParams.MatchParent表示layout参数的宽度和高度。这两个子项都是TextView,其中LayoutParameters=newlinearlayout.LayoutParams(0,ViewGroup.LayoutParams.WrapContent,parcentageOfChildWidth)。 我将padding Top添加到第一个子项TextView,但这样做,第二个
LinearLayout
中有两个子项,LinearLayout
使用ViewGroup.LayoutParams.MatchParent
表示layout参数的宽度和高度。这两个子项都是TextView
,其中LayoutParameters=newlinearlayout.LayoutParams(0,ViewGroup.LayoutParams.WrapContent,parcentageOfChildWidth)
。
我将padding Top添加到第一个子项TextView
,但这样做,第二个子项也会获得padding Top并向下推内容
我想知道是否有一种方法可以将padding Top添加到第一个子元素,而其余子元素保持原样。
第一个文本视图包含材质图标,第二个包含普通文本。您可以这样做:
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);
谢谢@Amin,为我指引了正确的方向!布局重力和重力都是TextView和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);
您使用的文本视图的重力布局和线性布局的重力布局是什么?谢谢您的回答。这可以很好地工作,但是图标在我的情况下是不可见的,当它不可见时我也需要保留空间。如果你想让它在不可见时保留空间,你只需要设置textView1.Visibility=ViewStates.invisible代码>