gpt4 book ai didi

java - Android 以编程方式设置 View 边距

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:05 25 4
gpt4 key购买 nike

我想以编程方式将边距设置为 View。这是我的 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llAttendeeList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000000">

<ListView
android:id="@+id/attendeelistview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#F6CECE" >
</ListView>

</LinearLayout>

以及当我的按钮onClick调出弹出窗口 View 时的方法:

private void openPopUp() {
LayoutInflater layoutInflater = (LayoutInflater) getActivity()
.getBaseContext().getSystemService(
context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.event_attendee_pop,
null);
llAttendeeList = (LinearLayout) popupView
.findViewById(R.id.llAttendeeList);
attendeeListView = (ListView) popupView.findViewById(R.id.attendeelistview);


LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(10, 10, 10, 0);
popupView.setLayoutParams(params);

final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, 350);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());

popupWindow.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
popupWindow.dismiss();
return true;
}
return false;
}
});
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
mAdapter = new ListAdapter(getActivity());
attendeeListView.setAdapter(mAdapter);
}

我尝试为 popupView 设置边距,但没有成功。 margin 不存在。我想知道哪一部分覆盖它。

最佳答案

手动设置 PopupWindow 的宽度和高度:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

popupWindow.setWidth(width-10);
popupWindow.setHeight(height-10);

关于java - Android 以编程方式设置 View 边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27664410/

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