簡體   English   中英

自定義 Xml 文件的功能未在 activity_main.xml 中實現

[英]Custom Xml file's functionality is not implementing in activity_main.xml

I am new in android development and I have created an xml file of name of "round_btn_bg.xml" in which I am just set the styling of my button but when I use this xml file in activity_main.xml the functionality of button is not loading在應用...

這是“round_btn_bg.xml”的代碼

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="@color/colorAccent"/>
    <corners android:radius="60dp"/>
</shape> 

下面是“actvity_main.xml”的代碼

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBlack"
    android:id="@+id/mainActivity_RL"
    tools:context=".MainActivity">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainActivity_BackgroundIV"
    android:src="@drawable/backgroundconnecteach"
    android:scaleType="centerCrop"
    android:alpha=".6"/>

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/mainActivity_logoIV"
        android:src="@drawable/logo"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:layout_marginTop="160dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mainActivity_TagLineTV"
        android:text="@string/get_started_tag_line"
        android:textColor="@color/colorWhite"
        android:textStyle="bold"
        android:layout_centerInParent="true"
        android:layout_below="@id/mainActivity_logoIV"/>
    
    <Button
        style="@drawable/round_btn_bg"
        android:id="@+id/mainActivity_MoveBtn"
        android:layout_width="168dp"
        android:layout_height="40dp"
        android:layout_below="@id/mainActivity_TagLineTV"
        android:layout_centerInParent="true"
        android:layout_marginTop="8dp"
        android:background="@drawable/round_btn_bg"
        android:text="@string/get_started_btn" />


</RelativeLayout>

在您聲明的 Button 中,您不能將樣式設置為可繪制資源。 它必須指向 styles.xml 中定義的樣式。 您將其設置為背景是正確的。

從 XML 中的 Button 中刪除以下行

style="@drawable/round_btn_bg"

為此使用背景屬性.. 第一次在可繪制文件中創建您的設計,如下所示

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <shape android:shape="oval">
        <solid android:color="#800013"/>
        <corners android:radius="5dp"/>
    </shape>
</item>

<item>
    <shape android:shape="oval">
        <solid android:color="#AA3B18"/>
        <corners android:radius="5dp"/>
    </shape>
</item>
然后將此添加為任何小部件(如按鈕)的背景
background="@drawable/round_btn_bg"
<Button
        android:id="@+id/mainActivity_MoveBtn"
        android:layout_width="168dp"
        android:layout_height="40dp"
        android:layout_below="@id/mainActivity_TagLineTV"
        android:layout_centerInParent="true"
        android:layout_marginTop="8dp"
        android:background="@drawable/round_btn_bg"
        android:text="@string/get_started_btn" />

用這個替換Button代碼。 我認為它會起作用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM