In android custom_toast_layout, layout margin is not working

大兔子大兔子 提交于 2019-12-11 14:49:19

问题


I am using the custom toast and in it i am providing layout_margin right but it is not working any suggestion where is the problem.below is the code i am using.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_layout_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF"
android:orientation="horizontal"
android:layout_marginRight="200dip">

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="5dp" />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#000" />
</LinearLayout>

回答1:


You canot set margin right on the top parnet while its fill parent.

I guess you want something like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_layout_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF"
android:orientation="horizontal">

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="5dp" />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
android:layout_marginRight="200dip"
    android:textColor="#000" />
</LinearLayout>



回答2:


Simply house your layout inside another layout, as below. and set background to transparent.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="62dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:padding="2dp"
        android:background="@drawable/bg_round_corner_toast"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/ic_gcm_notification"
            android:scaleType="fitCenter"
            android:layout_margin="10dp"
            android:layout_gravity="center_vertical"
            android:layout_weight="2"/>
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="8">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ixprez"
                android:textStyle="italic"
                android:textSize="12sp"
                android:id="@+id/tv_toast_title"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:text="Messsage "
                android:layout_margin="1dp"
                android:maxLines="3"
                android:gravity="top"
                android:layout_gravity="left|top"
                android:textSize="14sp"
                android:textStyle="normal"
                android:textAlignment="center"
                android:textColor="@color/white"
                android:id="@+id/tv_toast_msg"
                android:layout_below="@+id/tv_toast_title"
                />

        </RelativeLayout>

    </LinearLayout>


</LinearLayout>



回答3:


I know this is not exactly the same use, but you can replace margin by padding, and it works. In your case:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_layout_id"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFF"
    android:orientation="horizontal"
    android:layout_paddingRight="200dip">

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="5dp" />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#000" />
</LinearLayout>


来源:https://stackoverflow.com/questions/22120254/in-android-custom-toast-layout-layout-margin-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!