我正在尝试构建一个简单的circleView,其中有两个TextViews和一个分隔符。我正在尝试不使用XML来完成它。
首先,我将circleView添加到一个占用整个屏幕空间的LinearLayout中。当TextView是ALIGN_PARENT_TOP时,它的行为就像我所期望的那样(它坚持在circleView的顶部),但是当TextView有ALIGN_PARENT_BOTTOM时,circleView在整个屏幕高度上垂直延伸。为什么有这种行为?
我的代码:
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工作,或者有更好的方法来实现我想要的?
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();
}
}
发布于 2022-07-29 09:36:57
我修改了您的代码,用LinearLayout
代替了RelativeLayout
,并将Gravity
添加到LinearLayout
中,这样您的UI问题就解决了。我已经附上了下面修改过的代码。
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();
}
}
希望你有你的解决方案:)
https://stackoverflow.com/questions/73161991
复制相似问题