gpt4 book ai didi

android - 向动态 Android TextView 添加边距

转载 作者:行者123 更新时间:2023-11-29 18:09:02 28 4
gpt4 key购买 nike

我已经查看了关于此的大量以前的答案,并尝试了标记为有效的代码,但到目前为止,我所做的一切都无法说服我在 runtme 创建的 TextView 从左到右完全填充屏幕。我想在两侧添加边距,类似于普通 Toast。之后,我可以为形状添加阴影。

这是我的表单布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rel_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center_vertical|center_horizontal"
android:text="@string/hello_world"
android:textColor="@color/holobrightblue"
android:textSize="48sp"
android:textStyle="bold"
tools:context=".MainActivity" />

</RelativeLayout>

..和我的代码

textView = new TextView(m_Context);
RoundRectShape rs = new RoundRectShape(new float[] { 10, 10, 10, 10, 10, 10, 10, 10 }, null, null);

ShapeDrawable sd = new ShapeDrawable(rs);
sd.setAlpha(m_opacity);
textView.setBackgroundDrawable(sd);
textView.setTextColor(m_txtcolor);
textView.setText(toasttitle+"\n"+toastmessage);
textView.setBackgroundColor(Color.BLACK);
textView.setPadding(10,10,10,10);

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);


lp.setMargins(60, 0, 60, 0);
textView.setLayoutParams(lp);

toastView = new Toast(m_Context);
toastView.setView(textView);
toastView.setDuration(m_toastlen);
toastView.setGravity(m_screengravity, 0,0);
toastView.show();

如前所述,我从其他解决方案中尝试过的任何事情似乎都无法说服 textview 填充所有水平空间。

我试过删除那个形状等...

有什么想法吗?

最佳答案

您将需要包含您的 TextView 的容器 RelativeLayout

然后您可以将持有者的重力设置为中心

RelativeLayout head2 = new RelativeLayout(this);
head2.setId(++myid);
RelativeLayout.LayoutParams head2Params = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, 25);
head2Params.addRule(RelativeLayout.BELOW, head1.getId());
head2.setLayoutParams(head2Params);
head2.setBackgroundColor(Color.CYAN);

然后创建 TextView

textView = new TextView(m_Context);
RoundRectShape rs = new RoundRectShape(new float[] { 10, 10, 10, 10, 10, 10, 10, 10 }, null, null);

ShapeDrawable sd = new ShapeDrawable(rs);
sd.setAlpha(m_opacity);
textView.setBackgroundDrawable(sd);
textView.setTextColor(m_txtcolor);
textView.setText(toasttitle+"\n"+toastmessage);
textView.setBackgroundColor(Color.BLACK);
textView.setPadding(10,10,10,10);

LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
^^^^^^^^^^^^
^^^^^^^^^^^^MUST EDIT
lp.setMargins(60, 0, 60, 0);
textView.setLayoutParams(lp);

然后将您的 Textview 添加到具有中心重力的 holder relativeLayout

head2.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
head2.addView(textView);

显示它

toastView = new Toast(m_Context);
toastView.setView(textView);
toastView.setDuration(m_toastlen);
toastView.setGravity(m_screengravity, 0,0);

关于android - 向动态 Android TextView 添加边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11757860/

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