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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在其线性布局垂直父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);

enter image description here

答案

试试这个:

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);
}

Screenshot

另一答案

我通过为每个视图创建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中将以编程方式创建的单选按钮居中的主要内容,如果未能解决你的问题,请参考以下文章

将视图添加到底部的垂直线性布局(以编程方式)

以编程方式设置边距不起作用

将编程创建的视图垂直添加到滚动视图中(iOS 中的线性布局)

Android UI布局-1.1线性布局-线性布局基础

Android布局

android 线性布局 居右