【问题标题】:Center a programmatically created radio button in its linear layout vertical parent Android在其线性布局垂直父 Android 中以编程方式创建的单选按钮居中
【发布时间】:2017-12-27 02:42:52
【问题描述】:

我正在以编程方式创建一个线性布局,方向 = 垂直。 在该线性布局中,我还以编程方式创建 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);

【问题讨论】:

  • 提供你的xml代码
  • 你的 xml 在哪里?
  • 我正在以编程方式创建所有东西,这里没有 xml
  • 你设置Gravity.CENTER了吗?
  • 以编程方式使用 set Gravity 尝试一次 yourView.setGravity(Center)

标签: android android-layout radio-button android-linearlayout center


【解决方案1】:

试试这个:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
params.gravity = Gravity.CENTER;

yourview.setLayoutParams(params);

【讨论】:

  • 我试过这个,所有视图都在中心,但图像视图已调整为更小的尺寸!
  • 正如我在您的代码中看到的,您在 textview 中设置了重力。在屏幕截图中,我们可以看到 textview 居中。您根本没有将重力添加到单选按钮(在您的情况下为 Box)。尝试也为单选按钮添加重力。
  • 我尝试将重力 = 中心设置为我的单选按钮,但它不起作用:(
【解决方案2】:

试试下面的代码:-

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

【讨论】:

    【解决方案3】:

    我通过为每个视图创建 LayoutParams 并将其与其视图一起添加到父线性布局来修复它。

    LinearLayout.LayoutParams center = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent)
                                        {
                                            Gravity = GravityFlags.Center
                                        };
    

    【讨论】:

      【解决方案4】:

      试试这个

      在所有子视图中使用布局重力

      android:layout_gravity="center"
      

      【讨论】:

      • OP 不能。因为他在动态添加视图。
      【解决方案5】:

      使用

      yourLinearLayout.setGravity(Gravity.CENTER);
      

      检查这是否有效。

      【讨论】:

      • 我尝试为线性布局和单选按钮设置重力 = 中心,但没有帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      • 2012-07-10
      • 1970-01-01
      • 2017-12-25
      相关资源
      最近更新 更多