gpt4 book ai didi

android - 在 alertdialog 中添加 TextView 边距/填充

转载 作者:行者123 更新时间:2023-11-29 23:18:24 27 4
gpt4 key购买 nike

我正在尝试在 alertdialog 中为我的 TextView 添加边距。

AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); 
builder.setTitle(notificationList.get(position).getNotificationTitle());
final TextView tv = new TextView(getContext());
tv.setInputType( InputType.TYPE_CLASS_NUMBER );
tv.setText(notificationList.get(position).getNotificationMessage());
builder.setView(tv);

我正在尝试将我的脚本更改为此

AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(notificationList.get(position).getNotificationTitle());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(10,10,10,10);
final TextView tv = new TextView(getContext());
tv.setLayoutParams(params);
tv.setInputType( InputType.TYPE_CLASS_NUMBER );
tv.setText(notificationList.get(position).getNotificationMessage());
builder.setView(tv);

但仍然没有帮助,那么我如何为我的 textview 添加边距?

最佳答案

试试这个。当您添加 textView 时,您不需要 tv.setInputType(InputType.TYPE_CLASS_NUMBER);

    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle((notificationList.get(position).getNotificationTitle());
final TextView tv = new TextView(mContext);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.height = 100;
layoutParams.width = FrameLayout.MarginLayoutParams.MATCH_PARENT;
layoutParams.setMargins(50, 20, 50, 10);
tv.setText(notificationList.get(position).getNotificationMessage());
tv.setGravity(Gravity.CENTER);
builder.setView(tv);
builder.create().show();
tv.setLayoutParams(layoutParams);

您可以根据需要在 layoutParams.setMargins(int left, int top, int right, int bottom) 中更改边距。mContext 是您在其中使用警报对话框的 Activity 的上下文。因此,请确保该 Activity 应具有主题 Theme.AppCompat。或者您可以更改应用的样式。

例如:

public class MainActivity extends AppCompatActivity {

private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
}
}

现在您可以使用 mContext 作为当前 Activity 或 Activity 中后续 fragment 的上下文。

关于android - 在 alertdialog 中添加 TextView 边距/填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54863462/

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