gpt4 book ai didi

android - 对话框在android中 move ?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:13:05 24 4
gpt4 key购买 nike

我一直在 move 对话框。在这里,我发布了完整的源代码和屏幕截图。为什么我的对话框丢失了一部分。

代码:

public class MoveDialogBox extends Activity 
{
Dialog dialog;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
showDialog(1);
}
});
}
@Override
protected Dialog onCreateDialog( int id )
{
switch ( id )
{
case 1:
AlertDialog.Builder builder = null;
Context mContext = this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.info, (ViewGroup) findViewById(R.id.root));
Button exit = (Button) layout.findViewById(R.id.exit);
exit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
dismissDialog(1);
}
});
final LinearLayout LL = (LinearLayout) layout.findViewById(R.id.root);
LL.bringToFront();
LL.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
double X = event.getX();
double Y = event.getY();
String.valueOf(event.getX() + "x" + String.valueOf(event.getY()));
Toast.makeText(getBaseContext(), X + " : " + Y, Toast.LENGTH_SHORT).show();
LL.bringToFront();
LL.setX((float) X);
LL.setY((float) Y);
return false;
}
});
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
dialog = builder.create();
dialog.setCanceledOnTouchOutside(true);
break;
}
return dialog;
}
}

XML 代码:info.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_light"
android:orientation="horizontal" >
<TextView
android:id="@+id/about_Title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textSize="25dip"
android:textColor="@android:color/white"
android:textStyle="bold|italic"
android:text="Android" />
<Button
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Exit" />
</LinearLayout>

XML 代码:main.xml

<LinearLayout 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"
android:background="@drawable/androidimage" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Dialog Box" />
</LinearLayout>

屏幕截图:1
enter image description here
屏幕截图:2
enter image description here
屏幕截图:3
enter image description here
请大家帮忙看看如何把对话框移到整个屏幕上。

我哪里错了。怎么解决。

最佳答案

您正在 move 对话框内的布局,您需要 move 对话框本身。

WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
layoutParams.x = youNewX;
layoutParams.y = yourNewY;
dialog.getWindow().setAttributes(layoutParams);

关于android - 对话框在android中 move ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11930689/

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