gpt4 book ai didi

java - Android 对话框 wrap_content 不工作

转载 作者:行者123 更新时间:2023-11-29 04:42:31 26 4
gpt4 key购买 nike

我设置了一个自定义对话框,并将布局的宽度设置为 wrap_content但它仍然占用整个屏幕宽度。我什至尝试将宽度设置为 100dp 但它仍然使用整个屏幕 为什么会发生这种情况?

I have set up the dialog like this:

public class DialogGoToLine extends Dialog
{
//The root view of the dialog
private static View rootView;

public DialogGoToLine(Context context, int themeResId){
super(context, themeResId);
}

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

//Allow the dialog to be canceled by tapping out of its bounds
setCancelable(true);

//Inflate the custom layout
LayoutInflater layoutInflater = LayoutInflater.from(HotspotHelper.getAppContext());
rootView = layoutInflater.inflate(R.layout.dialog_go_to_line, null);

//Set up the layout
//setTitle(HotspotHelper.getAppContext().getString(R.string.clientDetails));
//setTitle("Go to line");
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(rootView);

final EditText editText = (EditText) rootView.findViewById(R.id.editTextGoToLine);

Button buttonGoToLine = (Button) rootView.findViewById(R.id.buttonGoToLine);
buttonGoToLine.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View p1)
{
int line = Integer.valueOf(editText.getText().toString()) - 1;
((LinearLayoutManager)LogActivity.recyclerViewLog.getLayoutManager()).scrollToPositionWithOffset(line, (int)HotspotHelper.dpToPx(30));
dismiss();
}
});
}
}

And the layout XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:orientation="horizontal">

<TextView
android:id="@+id/textViewGoToLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:labelFor="@+id/editTextGoToLine"
android:text="Go to line: "
android:background="@color/accent"
android:textColor="@color/primaryText"
android:textSize="18sp"/>

<EditText
android:id="@id/editTextGoToLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_margin="4dp"
android:layout_centerVertical="true"
android:inputType="number"
android:gravity="center"
android:textSize="18sp"/>

<Button
android:id="@+id/buttonGoToLine"
android:layout_width="wrap_content"
android:text="Go"
android:layout_margin="4dp"
android:textSize="18sp"
android:layout_height="wrap_content"/>

编辑 1如果它有所作为,这个对话框有一种风格,我在其中声明了这个可绘制对象的背景:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/colorPrimary"/>
<corners
android:radius="2dp" />
</shape>

最佳答案

使用必须使用此方法 setContentView(View view, ViewGroup.LayoutParams params) 而不是 setContentView(rootView);

ViewGroup.LayoutParams params= new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//ViewGroup.LayoutParams params= new ViewGroup.LayoutParams(
// 300,
// 300);
//for coustom size
params.height = thumb_size;
params.width = thumb_size;
// set padding ,margins ,gravity as your desired

使用这个setContentView(rootView, params);

关于java - Android 对话框 wrap_content 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38604242/

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