- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要以编程方式创建一个使用 ImageView 和 TextView 的 Toast,它出现在屏幕中间,我已经做到了。
这就是我想要的渲染
(带心形的黑色方 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 代码呈现的:
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/
我使用的是相对布局,有 4 个按钮(在中间,一个在另一个下面)。 我的问题是调整所有按钮之间的间距,使所有按钮之间的间距相同。 我想根据屏幕的高度动态地执行此操作(我使用 Display 类来获取实际
我的 android 应用程序中有以下模板。当我使用 android:layout_MarginTop="20dip"时效果很好,我可以在布局顶部看到 20dip 空间。
在 Android 中,您能否创建一个监听器来捕捉 View 属性的变化(宽度/高度/边距/相对于屏幕顶部的位置)? 我想在 layout_marginTop="10dp"更改为不同的值时触发一个事件
我需要以编程方式创建一个使用 ImageView 和 TextView 的 Toast,它出现在屏幕中间,我已经做到了。 这就是我想要的渲染 (带心形的黑色方 block 是ImageView图片,“
因此,我允许用户在屏幕周围拖动包含在 ImageView 中的 RelativeLayout(以及其他一些内容)。该布局将 ImageView 包裹在其中,还有另一个相对布局将内容放在 ImageVi
我是一名优秀的程序员,十分优秀!