自定义的 declare-styleable 未显示文本属性

huangapple go评论78阅读模式
英文:

Custom declare-styleable not displaying text attribute

问题

以下是您提供的内容的翻译:

我一直在开发一个定制的“按钮”,到目前为止,它的工作方式与我想要的一样。然而,我正在尝试在按钮上添加文本,而不是以前所做的可绘制对象。

目前,我在我的attrs.xml文件中使用了一个declare-styleable,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 按钮的自定义属性 -->
    <declare-styleable name="StyledButton">
        <attr name="cornerRadius" format="dimension" />
        <attr name="borderWidth" format="dimension" />
        <attr name="startColor" format="color" />
        <attr name="centerColor" format="color" />
        <attr name="endColor" format="color" />
    </declare-styleable>
</resources>

除此之外,我还有一个名为StyledButton.java的相关类:

package com.example.test;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.graphics.Path;
import android.graphics.Paint;
import android.graphics.LinearGradient;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageButton;

public class StyledButton extends AppCompatImageButton {

    // ...
    // 这里省略了类的其他部分,为了节省篇幅
    // ...

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // ...
        // 这里省略了绘制路径和画笔的其他部分,为了节省篇幅
        // ...
    }
}

我正试图在自定义对话框中使用这些内容,以获取用户输入,代码如下:

<?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"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:elevation="5dp">

    <androidx.cardview.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="20dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@drawable/background_gradient"
            android:padding="10dp">

            <TextView
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:scaleType="center"
                android:text="@string/app_name"
                android:textAlignment="center"
                android:fontFamily="@font/lato_bold"
                android:textSize="36sp"
                android:padding="10dp"
                android:textColor="@android:color/white" />

            <EditText
                android:id="@+id/player_name_dialog_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:layout_marginBottom="4dp"
                android:hint="@string/player_name_hint"
                android:textAlignment="center" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:orientation="horizontal"
                android:layout_gravity="center"
                android:layout_marginTop="10dp">

                <com.example.test.StyledButton
                    android:id="@+id/player_name_dialog_confirm_button"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:layout_width="120dp"
                    android:scaleType="center"
                    android:layout_gravity="center"
                    app:startColor="@color/colorPrimaryDark"
                    app:centerColor="@color/colorPrimary"
                    app:endColor="@color/colorAccent"
                    app:borderWidth="2dp"
                    app:cornerRadius="20dp"
                    android:text="@string/cancel"
                    android:textColor="@android:color/white"
                    android:textSize="20sp"
                    android:fontFamily="@font/lato"
                    android:layout_marginHorizontal="10dp"
                    android:onClick="deletePlayerButtonClick" />

                <com.example.test.StyledButton
                    android:id="@+id/add_players_activity_delete_button"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:layout_width="120dp"
                    android:scaleType="center"
                    android:layout_gravity="center"
                    app:startColor="@color/colorPrimaryDark"
                    app:centerColor="@color/colorPrimary"
                    app:endColor="@color/colorAccent"
                    app:borderWidth="2dp"
                    app:cornerRadius="20dp"
                    android:layout_marginHorizontal="10dp"
                    android:onClick="deletePlayerButtonClick" />

            </LinearLayout>
        </LinearLayout>
    </androidx.cardview.widget.CardView>
</RelativeLayout>

我尝试在attrs.xml文件中添加android:text,但这不会显示出来,我不明白为什么会这样。

提前谢谢!

英文:

I have been working on a custom "Button" and it works just as I want it to so far. However, I am trying to add text to the button instead of a drawable (which is what I was doing before).

Currently I have used a declare-styleable in my attrs.xml file which looks like the following:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;resources&gt;
&lt;!-- Custom attributes for the button --&gt;
&lt;declare-styleable name=&quot;StyledButton&quot;&gt;
&lt;attr name=&quot;cornerRadius&quot; format=&quot;dimension&quot; /&gt;
&lt;attr name=&quot;borderWidth&quot; format=&quot;dimension&quot; /&gt;
&lt;attr name=&quot;startColor&quot; format=&quot;color&quot; /&gt;
&lt;attr name=&quot;centerColor&quot; format=&quot;color&quot; /&gt;
&lt;attr name=&quot;endColor&quot; format=&quot;color&quot; /&gt;
&lt;/declare-styleable&gt;
&lt;/resources&gt;

Alongside this, I have the accompanying class StyledButton.java :

package com.example.test;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.graphics.Path;
import android.graphics.Paint;
import android.graphics.LinearGradient;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageButton;
public class StyledButton extends AppCompatImageButton {
private float cornerRadius = 0f;
private float borderWidth = 0f;
private int startColor = 0;
private int centerColor = 0;
private int endColor = 0;
private Path path = new Path();
private Paint borderPaint = new Paint();
{
borderPaint.setStyle(Paint.Style.FILL);
}
public StyledButton(Context context, @Nullable AttributeSet attrs) {
super(context, attrs, 0);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StyledButton);
try {
cornerRadius = a.getDimension(R.styleable.StyledButton_cornerRadius, 10f);
borderWidth = a.getDimension(R.styleable.StyledButton_borderWidth, 10f);
startColor = a.getColor(R.styleable.StyledButton_startColor, getResources().getColor(R.color.colorPrimaryDark, context.getTheme()));
centerColor = a.getColor(R.styleable.StyledButton_centerColor, getResources().getColor(R.color.colorAccent, context.getTheme()));
endColor = a.getColor(R.styleable.StyledButton_endColor, Color.WHITE);
}
finally {
a.recycle();
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
borderPaint.setShader(new LinearGradient(0f, 0f, 0f, (float) getHeight(), new int[] {startColor, centerColor, endColor}, null, Shader.TileMode.CLAMP));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
path.rewind();
path.addRoundRect(borderWidth, borderWidth, ((float) getWidth()) - borderWidth, ((float) getHeight()) - borderWidth, cornerRadius - borderWidth / 2, cornerRadius - borderWidth / 2, Path.Direction.CCW);
canvas.clipOutPath(path);
path.rewind();
path.addRoundRect(0f, 0f, ((float) getWidth()), ((float) getHeight()), cornerRadius, cornerRadius, Path.Direction.CCW);
canvas.drawPath(path, borderPaint);
}
}

I am trying to use these in a custom dialog to get user input with the following code:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:orientation=&quot;vertical&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center&quot;
android:elevation=&quot;5dp&quot;&gt;
&lt;androidx.cardview.widget.CardView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
app:cardCornerRadius=&quot;20dp&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;vertical&quot;
android:background=&quot;@drawable/background_gradient&quot;
android:padding=&quot;10dp&quot;&gt;
&lt;TextView
android:layout_width=&quot;300dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:scaleType=&quot;center&quot;
android:text=&quot;@string/app_name&quot;
android:textAlignment=&quot;center&quot;
android:fontFamily=&quot;@font/lato_bold&quot;
android:textSize=&quot;36sp&quot;
android:padding=&quot;10dp&quot;
android:textColor=&quot;@android:color/white&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/player_name_dialog_name&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;16dp&quot;
android:layout_marginLeft=&quot;4dp&quot;
android:layout_marginRight=&quot;4dp&quot;
android:layout_marginBottom=&quot;4dp&quot;
android:hint=&quot;@string/player_name_hint&quot;
android:textAlignment=&quot;center&quot;/&gt;
&lt;LinearLayout
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;40dp&quot;
android:orientation=&quot;horizontal&quot;
android:layout_gravity=&quot;center&quot;
android:layout_marginTop=&quot;10dp&quot;&gt;
&lt;com.example.test.StyledButton
android:id=&quot;@+id/player_name_dialog_confirm_button&quot;
android:layout_weight=&quot;1&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_width=&quot;120dp&quot;
android:scaleType=&quot;center&quot;
android:layout_gravity=&quot;center&quot;
app:startColor=&quot;@color/colorPrimaryDark&quot;
app:centerColor=&quot;@color/colorPrimary&quot;
app:endColor=&quot;@color/colorAccent&quot;
app:borderWidth=&quot;2dp&quot;
app:cornerRadius=&quot;20dp&quot;
android:text=&quot;@string/cancel&quot;
android:textColor=&quot;@android:color/white&quot;
android:textSize=&quot;20sp&quot;
android:fontFamily=&quot;@font/lato&quot;
android:layout_marginHorizontal=&quot;10dp&quot;
android:onClick=&quot;deletePlayerButtonClick&quot;/&gt;
&lt;com.example.test.StyledButton
android:id=&quot;@+id/add_players_activity_delete_button&quot;
android:layout_weight=&quot;1&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_width=&quot;120dp&quot;
android:scaleType=&quot;center&quot;
android:layout_gravity=&quot;center&quot;
app:startColor=&quot;@color/colorPrimaryDark&quot;
app:centerColor=&quot;@color/colorPrimary&quot;
app:endColor=&quot;@color/colorAccent&quot;
app:borderWidth=&quot;2dp&quot;
app:cornerRadius=&quot;20dp&quot;
android:layout_marginHorizontal=&quot;10dp&quot;
android:onClick=&quot;deletePlayerButtonClick&quot;/&gt;
&lt;/LinearLayout&gt;
&lt;/LinearLayout&gt;
&lt;/androidx.cardview.widget.CardView&gt;
&lt;/RelativeLayout&gt;

I have tried adding the android:text in the attrs.xml file but this doesn't show up and I don't understand why this may be.

Thanks in advance !

答案1

得分: 1

以下是翻译好的部分:

The text will not show as the custom view extends AppCompatImageButton. AppCompatImageButton only displays an image.

public class StyledButton extends AppCompatImageButton {

If you require a button that shows text consider having two custom views, one for image and the other for text.

StyledImageButton:

public class StyledImageButton extends AppCompatImageButton {

StyledButton:

public class StyledButton extends AppCompatButton {

The rest of the code can remain the same.

NOTE: Android will not let you create duplicate attributes in attrs.xml. Create global attributes and reuse them. Like so:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="cornerRadius" format="dimension" />
    <attr name="borderWidth" format="dimension" />
    <attr name="startColor" format="color" />
    <attr name="centerColor" format="color" />
    <attr name="endColor" format="color" />

    <declare-styleable name="StyledButton">
        <attr name="cornerRadius" />
        <attr name="borderWidth" />
        <attr name="startColor" />
        <attr name="centerColor" />
        <attr name="endColor" />
    </declare-styleable>

    <declare-styleable name="StyledImageButton">
        <attr name="cornerRadius" />
        <attr name="borderWidth" />
        <attr name="startColor" />
        <attr name="centerColor" />
        <attr name="endColor" />
    </declare-styleable>
</resources>
英文:

The text will not show as the custom view extends AppCompatImageButton. AppCompatImageButton only displays an image.

public class StyledButton extends AppCompatImageButton {

If you require a button that shows text consider having two custom views, one for image and the other for text.

StyledImageButton:

public class StyledImageButton extends AppCompatImageButton {

StyledButton

public class StyledButton extends AppCompatButton {

The rest of the code can remain the same.

NOTE: Android will not let you create duplicate attributes in attrs.xml. Create global attributes and reuse them. Like so:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;resources&gt;
    &lt;attr name=&quot;cornerRadius&quot; format=&quot;dimension&quot; /&gt;
    &lt;attr name=&quot;borderWidth&quot; format=&quot;dimension&quot; /&gt;
    &lt;attr name=&quot;startColor&quot; format=&quot;color&quot; /&gt;
    &lt;attr name=&quot;centerColor&quot; format=&quot;color&quot; /&gt;
    &lt;attr name=&quot;endColor&quot; format=&quot;color&quot; /&gt;

    &lt;declare-styleable name=&quot;StyledButton&quot;&gt;
        &lt;attr name=&quot;cornerRadius&quot; /&gt;
        &lt;attr name=&quot;borderWidth&quot; /&gt;
        &lt;attr name=&quot;startColor&quot; /&gt;
        &lt;attr name=&quot;centerColor&quot; /&gt;
        &lt;attr name=&quot;endColor&quot; /&gt;
    &lt;/declare-styleable&gt;

    &lt;declare-styleable name=&quot;StyledImageButton&quot;&gt;
        &lt;attr name=&quot;cornerRadius&quot; /&gt;
        &lt;attr name=&quot;borderWidth&quot; /&gt;
        &lt;attr name=&quot;startColor&quot; /&gt;
        &lt;attr name=&quot;centerColor&quot; /&gt;
        &lt;attr name=&quot;endColor&quot; /&gt;
    &lt;/declare-styleable&gt;
&lt;/resources&gt;

huangapple
  • 本文由 发表于 2020年9月12日 01:43:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63851923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定