首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android RelativeLayout以编程方式ALIGN_PARENT_BOTTOM和marginBottom行为

Android RelativeLayout以编程方式ALIGN_PARENT_BOTTOM和marginBottom行为
EN

Stack Overflow用户
提问于 2022-07-29 05:26:16
回答 1查看 48关注 0票数 0

我正在尝试构建一个简单的circleView,其中有两个TextViews和一个分隔符。我正在尝试不使用XML来完成它。

首先,我将circleView添加到一个占用整个屏幕空间的LinearLayout中。当TextView是ALIGN_PARENT_TOP时,它的行为就像我所期望的那样(它坚持在circleView的顶部),但是当TextView有ALIGN_PARENT_BOTTOM时,circleView在整个屏幕高度上垂直延伸。为什么有这种行为?

我的代码:

代码语言:javascript
运行
复制
public class MainActivity extends AppCompatActivity {

  @RequiresApi(api = Build.VERSION_CODES.O)
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int screenHeight = Resources.getSystem().getDisplayMetrics().heightPixels;

    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    setContentView(layout);

    RelativeLayout circleView = new RelativeLayout(this);
    GradientDrawable gd = new GradientDrawable();
    gd.setSize(screenHeight/3, screenHeight/3);
    gd.setColor(Color.WHITE);
    gd.setStroke(10, Color.GRAY);
    gd.setCornerRadius(screenHeight/3/2);
    circleView.setBackground(gd);
    layout.addView(circleView);

    View divider = new View(this);
    divider.setBackgroundColor(Color.GRAY);
    RelativeLayout.LayoutParams dividerLP = new RelativeLayout.LayoutParams(screenHeight / 3 / 10 * 9, 3);
    dividerLP.addRule(RelativeLayout.CENTER_IN_PARENT);
    divider.setLayoutParams(dividerLP);

    circleView.addView(divider);

    TextView tvTop = new TextView(this);
    tvTop.setText("Top");
    tvTop.setTextSize(50);
    RelativeLayout.LayoutParams tvTopLP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    tvTopLP.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    tvTopLP.addRule(RelativeLayout.CENTER_HORIZONTAL);
    tvTopLP.topMargin = 100;
    tvTop.setLayoutParams(tvTopLP);

    circleView.addView(tvTop);

    TextView tvBottom = new TextView(this);
    tvBottom.setText("Bottom");
    tvBottom.setTextSize(50);
    RelativeLayout.LayoutParams tvBottomLP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    tvBottomLP.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); // this seems to break the layout
    tvBottomLP.addRule(RelativeLayout.CENTER_HORIZONTAL);
    tvBottomLP.bottomMargin = 100;
    tvBottom.setLayoutParams(tvBottomLP);
    circleView.addView(tvBottom);

    getSupportActionBar().hide();
  }
}

作为解决问题的尝试,我将circleView封装在一个所需高度的LinearLayout中。现在,circleView没有扩展,但是TextView的bottomMargin不工作。我怎样才能让bottomMargin工作,或者有更好的方法来实现我想要的?

代码语言:javascript
运行
复制
public class MainActivity extends AppCompatActivity {

  @RequiresApi(api = Build.VERSION_CODES.O)
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int screenHeight = Resources.getSystem().getDisplayMetrics().heightPixels;

    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    setContentView(layout);

    LinearLayout linearLayout1 = new LinearLayout(this);
    linearLayout1.setBackgroundColor(Color.CYAN);
    LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
    lp1.weight = 1;
    linearLayout1.setLayoutParams(lp1);
    layout.addView(linearLayout1);

    LinearLayout linearLayout2 = new LinearLayout(this);
    linearLayout2.setGravity(Gravity.CENTER);
    linearLayout2.setBackgroundColor(Color.YELLOW);
    LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
    lp2.weight = 1;
    linearLayout2.setLayoutParams(lp2);
    layout.addView(linearLayout2);

    RelativeLayout circleView = new RelativeLayout(this);
    GradientDrawable gd = new GradientDrawable();
    gd.setSize(screenHeight/3, screenHeight/3);
    gd.setColor(Color.WHITE);
    gd.setStroke(10, Color.GRAY);
    gd.setCornerRadius(screenHeight/3/2);
    circleView.setBackground(gd);
    linearLayout2.addView(circleView);

    View divider = new View(this);
    divider.setBackgroundColor(Color.GRAY);
    RelativeLayout.LayoutParams dividerLP = new RelativeLayout.LayoutParams(screenHeight / 3 / 10 * 9, 3);
    dividerLP.addRule(RelativeLayout.CENTER_IN_PARENT);
    divider.setLayoutParams(dividerLP);

    circleView.addView(divider);

    TextView tvTop = new TextView(this);
    tvTop.setText("Top");
    tvTop.setTextSize(50);
    RelativeLayout.LayoutParams tvTopLP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    tvTopLP.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    tvTopLP.addRule(RelativeLayout.CENTER_HORIZONTAL);
    tvTopLP.topMargin = 100;
    tvTop.setLayoutParams(tvTopLP);

    circleView.addView(tvTop);

    TextView tvBottom = new TextView(this);
    tvBottom.setText("Bottom");
    tvBottom.setTextSize(50);
    RelativeLayout.LayoutParams tvBottomLP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    tvBottomLP.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    tvBottomLP.addRule(RelativeLayout.CENTER_HORIZONTAL);
    tvBottomLP.bottomMargin = 100; // why this isn't working?
    tvBottom.setLayoutParams(tvBottomLP);

    circleView.addView(tvBottom);

    LinearLayout linearLayout3 = new LinearLayout(this);
    linearLayout3.setBackgroundColor(Color.BLUE);
    LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
    lp3.weight = 1;
    linearLayout3.setLayoutParams(lp3);
    layout.addView(linearLayout3);

    getSupportActionBar().hide();
  }
}
EN

回答 1

Stack Overflow用户

发布于 2022-07-29 09:36:57

我修改了您的代码,用LinearLayout代替了RelativeLayout,并将Gravity添加到LinearLayout中,这样您的UI问题就解决了。我已经附上了下面修改过的代码。

代码语言:javascript
运行
复制
  public class MainActivity extends AppCompatActivity {
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int screenHeight = Resources.getSystem().getDisplayMetrics().heightPixels;

        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        setContentView(layout);

        LinearLayout linearLayout1 = new LinearLayout(this);
        linearLayout1.setBackgroundColor(Color.CYAN);
        LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
        lp1.weight = 1;
        linearLayout1.setLayoutParams(lp1);
        layout.addView(linearLayout1);

        LinearLayout linearLayout2 = new LinearLayout(this);
        linearLayout2.setGravity(Gravity.CENTER);
        linearLayout2.setBackgroundColor(Color.YELLOW);
        LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
        lp2.weight = 1;
        linearLayout2.setLayoutParams(lp2);
        layout.addView(linearLayout2);

        LinearLayout circleView = new LinearLayout(this);
        circleView.setOrientation(LinearLayout.VERTICAL);
        circleView.setGravity(Gravity.CENTER)
        GradientDrawable gd = new GradientDrawable();
        gd.setSize(screenHeight/3, screenHeight/3);
        gd.setColor(Color.WHITE);
        gd.setStroke(10, Color.GRAY);
        gd.setCornerRadius(screenHeight/3/2);
        circleView.setBackground(gd);
        linearLayout2.addView(circleView);

        TextView tvTop = new TextView(this);
        tvTop.setText("Top");
        tvTop.setTextSize(50);
        RelativeLayout.LayoutParams tvTopLP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        tvTopLP.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        tvTopLP.addRule(RelativeLayout.CENTER_HORIZONTAL);
        tvTopLP.bottomMargin = 70;
        tvTop.setLayoutParams(tvTopLP);

        circleView.addView(tvTop);

        View divider = new View(this);
        divider.setBackgroundColor(Color.GRAY);
        RelativeLayout.LayoutParams dividerLP = new RelativeLayout.LayoutParams(screenHeight / 3 / 10 * 9, 3);
        dividerLP.addRule(RelativeLayout.CENTER_IN_PARENT);
        divider.setLayoutParams(dividerLP);

        circleView.addView(divider);

        TextView tvBottom = new TextView(this);
        tvBottom.setText("Bottom");
        tvBottom.setTextSize(50);
        RelativeLayout.LayoutParams tvBottomLP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        tvBottomLP.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        tvBottomLP.addRule(RelativeLayout.CENTER_HORIZONTAL);
        tvBottomLP.topMargin = 50; // why this isn't working?
        tvBottom.setLayoutParams(tvBottomLP);

        circleView.addView(tvBottom);

        LinearLayout linearLayout3 = new LinearLayout(this);
        linearLayout3.setBackgroundColor(Color.BLUE);
        LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
        lp3.weight = 1;
        linearLayout3.setLayoutParams(lp3);
        layout.addView(linearLayout3);

        getSupportActionBar().hide();
    }
}

希望你有你的解决方案:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73161991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档