在其线性布局垂直父Android中将以编程方式创建的单选按钮居中

智者不为愚者谋,勇者不为怯者死。这篇文章主要讲述在其线性布局垂直父Android中将以编程方式创建的单选按钮居中相关的知识,希望能为你提供帮助。
我正在以编程方式创建一个线性布局,其中orientation = vertical。在该线性布局中,我还以编程方式创建2个或3个对象:
1图像视图
2文本视图(如果需要,我创建此对象)
3单选按钮
问题是如何将单选按钮置于其父级中心?
编辑:我的代码在Xamarin.android C#中,但我认为很容易将其转换为java Android

LinearLayout mainLinearLayout = new LinearLayout(this) { Orientation = Orientation.Vertical}; LinearLayout sub1LinearLayout = new LinearLayout(this) { //Set orientation = horizontal Orientation = Orientation.Horizontal}; //Add the sub1LinearLayout to the mainLinearLayout mainLinearLayout.AddView(sub1LinearLayout); for (int j = 0; j < count; j++) {LinearLayout sub2LinearLayout = new LinearLayout(this) { Orientation = Orientation.Vertical }; LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent); RadioButton Box = new RadioButton(this){ //Set gravity = center Gravity = GravityFlags.Center }; switch (SomeMode) {//Show radio button + image case Mode.ShowBoxAndImage: {ImageView image = new ImageView(this); Bitmap imageBitmap = BitmapFactory.DecodeFile(path); image.SetImageBitmap(imageBitmap); sub2LinearLayout.AddView(image); layoutParams.SetMargins(padding, 0, 2 * padding, 0); break; } //Show radio button + its text + image case Mode.ShowBoxAndImageAndText: { ImageView image = new ImageView(this); Bitmap imageBitmap = BitmapFactory.DecodeFile(path); image.SetImageBitmap(imageBitmap); sub2LinearLayout.AddView(image); TextView Name = new TextView(this) {Text = name,TextSize = nameTextSize,//Set gravity = center Gravity = GravityFlags.Center }; Name.SetTextColor(new Color(ContextCompat.GetColor(this, Resource.Color.colorBlack))); sub2LinearLayout.AddView(Name); layoutParams.SetMargins(padding, 0, 2 * padding, 0); break; }} }sub2LinearLayout.AddView(Box); sub2LinearLayout.LayoutParameters = layoutParams; sub1LinearLayout.AddView(sub2LinearLayout); //Set sub2LinearLayout gravity = bottom sub2LinearLayout.SetGravity(GravityFlags.Bottom);

在其线性布局垂直父Android中将以编程方式创建的单选按钮居中

文章图片

答案试试这个:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.weight = 1.0f; params.gravity = Gravity.CENTER; yourview.setLayoutParams(params);

另一答案尝试以下代码: -
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); linearLayout = new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); linearLayout.setLayoutParams(params); linearLayout.setPadding(15, 15, 15, 15); setContentView(linearLayout); ImageView imageView = new ImageView(this); imageView.setImageResource(R.drawable.english); TextView textView = new TextView(this); textView.setGravity(Gravity.CENTER); textView.setText("1"); RadioButton button = new RadioButton(this); button.setGravity(Gravity.CENTER); button.setChecked(true); linearLayout.addView(imageView); linearLayout.addView(textView); linearLayout.addView(button); }

在其线性布局垂直父Android中将以编程方式创建的单选按钮居中

文章图片

另一答案我通过为每个视图创建LayoutParams来修复它,并将其视图添加到父线性布局。
LinearLayout.LayoutParams center = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent) { Gravity = GravityFlags.Center };

另一答案试试这个
在所有子视图中使用布局重力
android:layout_gravity="center"

另一答案使用
yourLinearLayout.setGravity(Gravity.CENTER);

【在其线性布局垂直父Android中将以编程方式创建的单选按钮居中】检查这是否有效。

    推荐阅读