使用FrameLayout作为根布局,在另一个视图的顶部添加具有X,Y偏移的视图 [英] With FrameLayout as root layout, add view on top of another one, with offset of X,Y
问题描述
我想将视图放置在现有视图之上.我定位的视图位于LinearLayout内部,该外观位于FrameLayout中.
I'd like to place a view on top of an existing view. The view I'm targeting is inside a LinearLayout, which resides in a FrameLayout.
我正在考虑使用RelativeLayout做到这一点,因为我已经使其部分工作了.我想将新视图对齐到左下角或左上角(作为原点),然后将X和Y偏移到我指定的某个精确值.
I'm thinking there's a way to do this with RelativeLayout because I already have it partially working. I'd like to align the new view to the bottom-left or top-left (as the origin) and then offset X and Y to some precise value that I specify.
如何实现?
这里是个主意:
public static void placeTextRelativeToBottomLeftOfViewAtXY(final FrameLayout layout, View component, int x, int y, String text) {
final TextView textView = new TextView(getContext());
textView.setId((int)System.currentTimeMillis());
final RelativeLayout relativeLayout = new RelativeLayout(getContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params.setMargins(x, y, 0,0);
params.addRule(RelativeLayout.LEFT_OF, component.getId());
relativeLayout.setLayoutParams(params);
relativeLayout.setBackgroundColor(Color.TRANSPARENT);
textView.setText("+500 points!");
textView.bringToFront();
relativeLayout.addView(textView, params);
layout.addView(relativeLayout);
}
推荐答案
基于注释中的其他信息,即使可以在FrameLayout
中重叠不同的布局,这些布局也只能将其位置自己的孩子.
RelativeLayout
将无法将其子视图之一相对于其他同级或父布局中的视图放置.
Based on the additional information in comments, even if it is possible to overlap a different layouts inside a FrameLayout
, those layouts will only be able to position their own children.
A RelativeLayout
won't be able to position one of its child views relative to a view in a different sibling or parent Layout.
可行的方法是平整布局的层次结构,将根布局设置为RelativeLayout
或ConstraintLayout
.
The way to go would be to flattern the heierarchy of Layouts, setting the root layout to a RelativeLayout
or a ConstraintLayout
.
ConstraintLayout
在定位视图方面更灵活,但也更难以学习.
ConstraintLayout
is more flexible in terms of positioning views, but it is also more difficult to learn.
在这里,我要留下一种替代方法,以供RelativeLayout
用作根视图.要查看的重要项目是LayoutParams
的设置,有时可能会引起混淆.
LayoutParams
是在子视图上设置的,但是所使用的类取决于父视图.
Here I am leaving an alternative to be used with RelativeLayout
as the root view. The important items to look at are the setting of the LayoutParams
which is sometimes a bit confussing.
The LayoutParams
are set on the child view, but the class used depends on the parent view.
还请记住,要保持页边空白独立显示,您需要将dp转换为像素(为简单起见,我没有这样做,但是在SO中有一些有关如何执行此操作的示例).
Also take in mind that to keep margins display independent you need to convert dp into pixels (for the sake of simplicity I haven't done that, but there are examples of how to do this here in SO).
它还使用View.generteViewId()
来获取动态创建的视图的ID.
It also uses View.generteViewId()
go get an id for a view created dynamically.
为简单起见,我在xml中包含了引用View
,但是我也可以动态创建.
To make it simple I included the reference View
in the xml, but i could have also been created dynamically.
布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvCenterText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Texto estatico"
android:layout_centerInParent="true"/>
</RelativeLayout>
主要活动
public class DynamicViewsActivity extends AppCompatActivity {
RelativeLayout rlContainer;
TextView centerText;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamicviews);
rlContainer = findViewById(R.id.rlContainer);
centerText = findViewById(R.id.tvCenterText);
placeTextRelativeToBottomLeftOfViewAtXY(rlContainer, centerText, 100,10, "Hola");
}
public void placeTextRelativeToBottomLeftOfViewAtXY(final RelativeLayout layout, View component, int x, int y, String text) {
final TextView textView = new TextView(this);
textView.setId(View.generateViewId());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(x, y, x,y);
params.addRule(RelativeLayout.LEFT_OF, component.getId());
params.addRule(RelativeLayout.ALIGN_BASELINE, component.getId());
textView.setLayoutParams(params);
textView.setText(text);
layout.addView(textView);
}
}
这篇关于使用FrameLayout作为根布局,在另一个视图的顶部添加具有X,Y偏移的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!