gpt4 book ai didi

android - 制作教程(教练标记)叠加。需要帮助根据另一个人的位置移动 View

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:16 27 4
gpt4 key购买 nike

你好,我正在尝试在外行教程上做一个教练标记。

主.java

public class Main extends Activity {

private Button button1;
private int x;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help_overlay);
button1 = (Button)findViewById(R.id.button1);
x = button1.getTop();


showOverLay();

}

private void showOverLay(){

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
final Dialog dialog = new Dialog(this, 0);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

dialog.setContentView(R.layout.overlay_view);

RelativeLayout layout = (RelativeLayout) dialog.findViewById(R.id.overlayLayout);

layout.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0)
{
Main.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
dialog.dismiss();

}

});

dialog.show();

}
}

基本上,showOverlay 方法在 Activity 首次启动时被调用。它显示了一个占据整个屏幕且具有透明背景且没有标题的对话框。下面看到的 overlay.xml 是这个对话框的布局。

我想做的是动态调整 TextView 的位置,使其位于按钮上方以补偿不同的设备屏幕(将来我将添加带有指向事物的箭头的 ImageView ,所以如果这些箭头不显示会很奇怪他们所指的观点)。

请注意。我不能使用 showCaseView,因为我将以 api 8 为目标。

enter image description here

上图显示了我想做的事情。将显示“HEEEEEELP”的 TextView 移动到按钮的略上方。

main_activity.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="96dp"
android:onClick="idk"
android:text="This Button" />
</RelativeLayout>

overlay.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:id="@+id/overlayLayout">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="HEEEEEEELP"
android:textColor="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

感谢阅读。

更新 2

我的 OnCreate 方法

 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help_overlay);
button1 = (Button)findViewById(R.id.button1);
button1.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
public boolean onPreDraw() {
top = button1.getTop();
bottom = button1.getBottom();
left = button1.getLeft();
right = button1.getRight();
showOverLay();
button1.getViewTreeObserver().removeOnPreDrawListener(this);
return true;
}
});


}

我现在可以访问 button1 的坐标。但是,我不确定如何将 TextView 放置在能够满足不同设备尺寸的按钮上方。我尝试使用 setMargins 方法的参数,但很难判断数量。我做了一些研究,发现我可以使用带有布局参数的相对布局并使用 ABOVE 等字段,但这似乎没有按预期工作。 TextView 不在按钮上方。

我的代码。

RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)textView.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_HORIZONTAL, R.id.button1);
lp.addRule(RelativeLayout.ABOVE, R.id.button1);

textView.setLayoutParams(lp);

请注意,我的overlay_view.xml已经变成了相对布局。

最佳答案

这是你应该做的:

button1.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
public boolean onPreDraw() {
button1.getViewTreeObserver().removeOnPreDrawListener(this);
x = button1.getTop();
showOverLay();
return true;
}
});

实际上,当您在 onCreate() 中调用 x = button1.getTop(); 时,Button View 尚未绘制和测量,因此 的值getTop 为 0。试试这个。

关于android - 制作教程(教练标记)叠加。需要帮助根据另一个人的位置移动 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26348560/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com