我需要以编程方式创建一个使用ImageView和TextView的Toast,它出现在屏幕中间,我已经完成了。
这是我想要的RENDER
(带有心脏的黑色方块是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代码的RENDER:
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);
我尝试过使用relativelayout或linearlayout,但framelayout对此更有效。 所以我有一个问题:
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);
需要帮助。
答案 0 :(得分: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);
答案 1 :(得分:0)
试试这个......
LinearLayout.LayoutParams paramsOfAnim = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsOfAnim.gravity = Gravity.center_horizontal;
paramsOfAnim.topMargin = 85;
答案 2 :(得分:0)
B.layoutParams
来描述B在A中的位置,而不是在B中放置项目,例如C. 作为here.