在alertdialog中添加textview margin / padding

问题描述 投票:1回答:2

我正在尝试为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添加保证金?

android android-alertdialog
2个回答
1
投票

试试这个吧。在添加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是您正在使用警报对话框的活动的上下文。因此,请确保该活动应具有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作为当前活动中的上下文或活动中的后续片段。


0
投票

请尝试下面的代码

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


LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.setMargins(10,10,10,10);
 tv.setLayoutParams(params);
ll.addView(tv);
builder.setView(ll);
© www.soinside.com 2019 - 2024. All rights reserved.