gpt4 book ai didi

java - 安卓自定义按钮

转载 作者:行者123 更新时间:2023-12-02 08:31:17 25 4
gpt4 key购买 nike

我想创建一个像这样的自定义按钮:

http://i77.photobucket.com/albums/j74/bertyhell/button.png?t=1279626910

但这不起作用我需要在按钮上显示文本,并相对于按钮原点进行绝对定位所有字符串都需要在运行时更改

但下面的按钮可以是普通按钮

我该怎么做?

如果我通过继承 android.widget.button 创建一个按钮,我必须自己绘制整个按钮?

有没有更简单的方法?

编辑

好吧,我尝试了从相对布局继承

像这样:

    package be.smarttelecom.datacheck.views;

import android.content.Context;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class AlertButton extends RelativeLayout {

private TextView roomNumber;

public AlertButton(Context context, String roomNumber) {
super(context);
try{
this.alertId = alertId;
// TODO Auto-generated constructor stub
this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));

TextView room = new TextView(context);
room.setFocusable(false);
room.setText(R.string.room);
room.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));

this.roomNumber = new TextView(context);
this.roomNumber.setFocusable(false);
this.roomNumber.setId(R.id.roomNumber);
this.roomNumber.setTextSize(26);
this.roomNumber.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
this.roomNumber.setText(roomNumber);

}catch(Exception ex){
Log.i("test", "problem during construction of alert button: " + ex.getMessage());
}
}

public void setRoomNumber(String number){
roomNumber.setText(number);
}

public void setDescription(String description){
this.description.setText(description);
}
}

当我像这样添加它时:

        TableRow row2 = new TableRow(this);
AlertButton alert = new AlertButton(this, "1","14","wc","krist");
row2.addView(alert);
container.addView(row2, 0);

我得到一个空白页面并且没有任何错误

知道我做错了什么吗?

如何将文本字段放在某些位置?将它们放在绝对布局中并添加 x 和 y 坐标?(绝对布局已被废弃,有替代方案吗?)

最佳答案

我认为最简单的方法是创建一个扩展RelativeLayout的自定义 View 。然后,您可以按照您希望的任何排列方式添加 4 个 TextView,并进行任何您需要的自定义。要使其外观和行为类似于按钮,您可以将按钮添加到布局中,并将宽度和高度设置为 fill_parent。确保您的 TextView 不可聚焦,并且按钮被发送到布局的后面。

或者,您可以将自定义 View 中的 RelativeLayout 属性设置为 clickable=true 和 focusable=true。然后添加一个看起来像按钮的可绘制对象作为 View 背景。使用选择器像真正的按钮一样处理所有聚焦/按下状态。有关如何执行此操作的详细信息可以在此处找到:Standard Android Button with a different color

关于java - 安卓自定义按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3289658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com