gpt4 book ai didi

java - android Layout_gravity(不是重力)和 Layout_marginTop(不是 marginTop)以编程方式

转载 作者:行者123 更新时间:2023-11-30 09:12:09 25 4
gpt4 key购买 nike

我需要以编程方式创建一个使用 ImageView 和 TextView 的 Toast,它出现在屏幕中间,我已经做到了。

这就是我想要的渲染

enter image description here

(带心形的黑色方 block 是ImageView图片,“J'aime”是TextView)

XML 代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
android:id="@+id/toastImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:src="@drawable/like" />

<TextView
android:id="@+id/toastText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="85dp"
android:text="J'aime !"
android:textColor="#FFFFFF" />

</FrameLayout>

这是我用 JAVA 代码呈现的:

enter image description here

Java 代码:

FrameLayout toastLayout = new FrameLayout(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
toastLayout.setLayoutParams(llp);

ImageView toastImg = new ImageView(this);
Bitmap toastBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.like);
toastImg.setImageBitmap(toastBitmap);

TextView toastText = new TextView(this);
toastText.setText("J'aime !");

LinearLayout.LayoutParams toastTextLp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
toastTextLp.setMargins(0, 85, 0, 0);
toastText.setLayoutParams(toastTextLp);

toastLayout.addView(toastImg);
toastLayout.addView(toastText);

Toast toast = new Toast(this);
toast.setView(toastLayout);

我尝试过使用相对布局或线性布局,但框架布局对此效果更好。所以我有一个问题:

什么是 JAVA 等价物:

    android:layout_gravity="center_horizontal"
android:layout_marginTop="85dp"

?因为这显然不是:

    LinearLayout.LayoutParams toastTextLp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
toastTextLp.setMargins(0, 85, 0, 0);

需要帮助。

最佳答案

试试这个:

RelativeLayout toastLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams llp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
toastLayout.setLayoutParams(llp);

ImageView toastImg = new ImageView(this);
Bitmap toastBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.like);
toastImg.setImageBitmap(toastBitmap);

TextView toastText = new TextView(this);
toastText.setText("J'aime !");

RelativeLayout.LayoutParams toastTextLp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
toastTextLp.addRule(RelativeLayout.CENTER_HORIZONTAL);
toastTextLp.setMargins(0, 85, 0, 0);
toastText.setLayoutParams(toastTextLp);

toastLayout.addView(toastImg);
toastLayout.addView(toastText);

Toast toast = new Toast(this);
toast.setView(toastLayout);

关于java - android Layout_gravity(不是重力)和 Layout_marginTop(不是 marginTop)以编程方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21752940/

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