2

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

这是我想要的渲染

在此处输入图像描述

(带心的黑色方块是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 代码的渲染:

在此处输入图像描述

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);

需要帮忙。

4

3 回答 3

2

试试这个:

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);
于 2014-02-13T11:45:14.257 回答
0

试试这个...

LinearLayout.LayoutParams paramsOfAnim = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    paramsOfAnim.gravity = Gravity.center_horizontal;
    paramsOfAnim.topMargin = 85;
于 2014-02-13T11:31:38.167 回答
0
  • 假设A包含B,B包含C。您设置B.layoutParams用于描述B在A中的放置,而不是用于在B中放置物品,例如C。
  • 另一件事是布局参数足够复杂,很难不忘记一些东西,而且你真的不需要手动设置所有参数。因此,最好阅读它们,更改并返回。

就像这里一样。

于 2014-02-13T11:48:18.860 回答