【问题标题】:android Layout_gravity (not gravity) and Layout_marginTop(not marginTop) programmaticallyandroid Layout_gravity(不是重力)和Layout_marginTop(不是marginTop)以编程方式
【发布时间】:2014-03-12 06:06:42
【问题描述】:

我需要以编程方式创建一个使用 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);

需要帮助。

【问题讨论】:

    标签: java android layout-gravity


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 谢谢我一直在尝试这个但没有 addRule 方法,这似乎可以解决问题!
    【解决方案2】:

    试试这个...

    LinearLayout.LayoutParams paramsOfAnim = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        paramsOfAnim.gravity = Gravity.center_horizontal;
        paramsOfAnim.topMargin = 85;
    

    【讨论】:

      【解决方案3】:
      • 假设 A 包含 B,B 包含 C。您设置 B.layoutParams 用于描述 B 在 A 中的放置,而不是用于在 B 中放置项目,例如 C。
      • 另外一点就是布局参数够复杂,很难不忘记,而且你真的不需要手动设置所有参数。因此,最好阅读它们,改变并退后。

      作为here.

      【讨论】:

        猜你喜欢
        • 2015-01-21
        • 2017-03-17
        • 1970-01-01
        • 2013-03-31
        • 2011-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-29
        相关资源
        最近更新 更多