在Service

时间:2017-11-29 04:30:01

标签: android xml android-activity service android-inflate

当试图像这样膨胀View时:

val lay: LayoutInflater = getSystemService(LAYOUT_INFLATER_SERVICE) as (LayoutInflater)
mView = lay.inflate(R.layout.dialog_new_order, null)

我看到一个膨胀的例外:

  

二进制XML文件行#0:无法解析索引1处的属性:   TypedValue {t = 0x2 / d = 0x7f030006 a = -1}

但是当在活动中写相同的行时,它正常工作!

查看XML:

<?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="wrap_content"
    android:layout_centerInParent="true"
    android:background="@color/white"
    android:layout_margin="10dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:drawableStart="@mipmap/ic_launcher"
        android:gravity="center_vertical"
        android:drawablePadding="20dp"
        android:text="@string/new_order"
        android:textColor="@color/white"
        android:textSize="@dimen/text_size_large" />

    <ImageView
        android:id="@+id/order_image"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp" />

    <TextView
        android:id="@+id/order_rest_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:textColor="@color/black"
        android:textSize="@dimen/text_size_xlarge" />

    <TextView
        android:id="@+id/order_customer_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:textColor="@color/black"
        android:textSize="@dimen/text_size_normal" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/bg_btns_border"
        android:orientation="horizontal"
        android:weightSum="1">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:padding="10dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/order_price"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:gravity="center"
                android:textSize="@dimen/text_size_large" />

            <TextView
                android:id="@+id/order_pay_method"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:gravity="center" />


        </LinearLayout>

        <View
            android:layout_width="0.5dp"
            android:layout_height="match_parent"
            android:background="@color/gray_lite" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:padding="10dp"

            android:orientation="vertical">

            <TextView
                android:id="@+id/order_delivery_time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="0 Min"
                android:singleLine="true"
                android:ellipsize="end"
                android:textColor="@color/black"
                android:textSize="@dimen/text_size_large" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textColor="@color/black"
                android:text="@string/to_restaurant" />
        </LinearLayout>
    </LinearLayout>

    <Button
        android:id="@+id/order_accept_btn"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_margin="10dp"
        android:background="@drawable/bg_btns_rounded_green"
        android:text="@string/order_status_start_pickup"
        android:textAllCaps="true"
        android:textSize="@dimen/text_size_large"
        android:textColor="@color/white" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

按照说法Alessio,这是正确的服务没有UI所以如果你想从你的服务中显示关于活动的视图然后通过,你就不能使用inflater您的Activity Context和展示View

LayoutInflater lay=(LayoutInflater)Context.getSystemService(LAYOUT_INFLATER_SERVICE)

mView = lay.inflate(R.layout.dialog_new_order, null)