在 Android studio 中的自定义视图编辑器中显示正确的布局

2024-03-30

我在 Android Studio 中遇到布局预览的奇怪行为。我开发了一个扩展 AppCompatButton 的自定义视图,并且布局是运行时的预期布局,但是布局预览中的布局未正确显示。特别是,该按钮已显示,但没有从自定义属性设置背景颜色,也没有drawableStart(根据名为“social”的自定义属性在自定义视图初始化中设置)。 请参阅下面的代码了解更多详细信息。 请注意,自定义视图位于作为模块导入主项目中的库内。 我在这里缺少什么?谢谢 :)

自定义视图代码:

public class SocialButton extends AppCompatButton {
    private static final String TAG = SocialButton.class.getSimpleName();
    Context mContext;
    int backgroundColor;
    int socialId;
    int textColor;

    public SocialButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.SocialButton,
                0, 0);
        initDefaults(a);
        setIconForSocial();
        initStateListDrawable();
    }

    public void initDefaults(TypedArray a){
        try {
            backgroundColor = a.getColor(R.styleable.SocialButton_backgroundColor, Color.WHITE);
            textColor = a.getColor(R.styleable.SocialButton_textColor,
                    ContextCompat.getColor(mContext,R.color.social_button_text_color));
            socialId = a.getInt(R.styleable.SocialButton_social, 0);
        } finally {
            a.recycle();
        }
    }

    public void initStateListDrawable(){
        StateListDrawable stateListDrawable = new StateListDrawable();
        GradientDrawable drawable;
        // Pressed, focused
        drawable =  new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setColor(ContextCompat.getColor(mContext,R.color.border_button_ripple_color));
        stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, drawable);
        stateListDrawable.addState(new int[]{android.R.attr.state_focused}, drawable);
        // Disabled
        drawable =  new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setColor(ContextCompat.getColor(mContext,R.color.raised_disabled_color));
        stateListDrawable.addState(new int[]{-android.R.attr.state_enabled}, drawable);
        // Normal
        drawable =  new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setColor(backgroundColor);
        stateListDrawable.addState(new int[]{android.R.attr.state_enabled}, drawable);
        setBackground(stateListDrawable);
        setTextColor(textColor);
        setAllCaps(false);
    }

    public void setBackgroundColor(int color){
        backgroundColor = color;
        initStateListDrawable();
    }

    private void setIconForSocial(){
        Drawable img;
        if (socialId == 0){
            img = getContext().getResources().getDrawable( R.drawable.ic_google );
        }else{
            img = getContext().getResources().getDrawable( R.drawable.ic_facebook );
        }
        //setPadding(LayoutUtils.dpToPx(27,mContext),LayoutUtils.dpToPx(27,mContext),
                //LayoutUtils.dpToPx(27,mContext),LayoutUtils.dpToPx(27,mContext));
        setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);
    }
}

布局中的自定义视图 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="#F5F5F5"
    android:paddingBottom="16dp"
    tools:context="it.zehus.bitridesharing.login.SignInActivity">

    <com.daimajia.slider.library.SliderLayout
        android:id="@+id/slider"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/gd40"
        app:layout_constraintDimensionRatio=""
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        tools:background="#800000FF" />

    <android.support.constraint.Guideline
        android:id="@+id/gd40"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.40" />


    <android.support.constraint.Guideline
        android:id="@+id/gd63"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.63" />

    <com.daimajia.slider.library.Indicators.PagerIndicator
        android:id="@+id/custom_indicator"
        style="@style/AndroidImageSlider_Magnifier_Oval_Green"
        android:layout_width="395dp"
        android:layout_height="27dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvCaption" />

    <com.zehus.customlayouts.buttons.BorderButton
        android:id="@+id/btCreateAccount"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginEnd="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="12dp"
        android:text="Create an account"
        android:textAllCaps="false"
        android:textSize="16sp"
        app:borderColor="@color/colorPrimary"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btFacebookLogin"
        app:textColor="@color/colorPrimary" />


    <com.zehus.customlayouts.buttons.SocialButton
        android:id="@+id/btFacebookLogin"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginEnd="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="12dp"
        app:social="Facebook"
        android:text="@string/continue_with_facebook"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btGoogleLogin" />

    <com.facebook.login.widget.LoginButton
        android:id="@+id/btPhantomFbLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="30dp"
        android:visibility="gone" />

    <com.zehus.customlayouts.buttons.SocialButton
        android:id="@+id/btGoogleLogin"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginEnd="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="8dp"
        android:text="@string/continue_with_google"
        app:social="Google"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/gd63">
    </com.zehus.customlayouts.buttons.SocialButton>

    <Button
        android:id="@+id/btSignIn"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="24dp"
        android:textColor="@color/colorPrimary"
        android:layout_marginStart="24dp"
        android:text="Sign in"
        android:textAllCaps="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/tvCaption"
        android:layout_width="wrap_content"
        android:layout_height="21dp"
        android:layout_marginTop="16dp"
        android:text="Hybrid vs Boost"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/slider" />

</android.support.constraint.ConstraintLayout>

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Android studio 中的自定义视图编辑器中显示正确的布局 的相关文章

  • 我们如何从 Android 通知中提取 bigpicturestyle 图像?

    我有一个通知侦听器服务 可以读取来自其他应用程序的通知 经用户许可 并提取所有数据 能够访问除通知展开视图中显示的图像之外的所有内容 我也在阅读 EXTRA PICTURE 意图值 if extras containsKey Notific
  • Realm 中的更新语句

    I have VisitingCardPOJO表格 我想更新单个条目说在哪里no 3 VisitingCardPOJO java public class VisitingCardPOJO extends RealmObject Prima
  • 模仿 youtube/gmail 应用程序的导航抽屉

    背景 近几个月来 谷歌发布了Youtube 应用程序 http www androidpolice com 2013 08 19 breaking massive youtube for android v5 0 update brings
  • 使用 PutDataMapRequest 后,Wearable.DataApi.getDataItem() 的 Uri 是什么?

    我正在测试可穿戴数据层 API如中所述安卓教程 http developer android com training wearables data layer index html 有一个基于底层 APIDataItem http dev
  • 在Android中使用RxJava2插入SQLiteDatabase

    我在学习RxJava2在安卓中 谁能解释一下我们如何使用将数据插入 SQLiteDatabaseRxJava2 这是我尝试使用的代码示例 但它将数据插入数据库六次 单击时 getCompletableObservable subscribe
  • Google 地图位于活动的中间区域

    我正在使用 Android studio 2 1 2 我检查了一下 大多数问题要么使用旧版本的 Android studio 要么使用一些旧的类 这些类不适用于我的情况 从文件 gt 新项目 gt 我使用了选项Google Maps Act
  • Android中如何使用JNI获取设备ID?

    我想从 c 获取 IMEIJNI 我使用下面的代码 但是遇到了未能获取的错误cls 它总是返回NULL 我检查了环境和上下文 它们都没有问题 为什么我不能得到Context班级 我在网上搜索了一下 有人说我们应该使用java lang Ob
  • Android 辅助功能服务检测通知

    我试图让我的应用程序在显示通知时进行检测 我已在设置应用程序中启用它并且onServiceConnected确实接到电话 但是当我创建通知或通过 gmail 应用程序接收电子邮件时 什么也没有发生 onAccessibilityEvent没
  • 如何在单个查询中搜索 RealmObject 的 RealmList 字段

    假设我有一堂课 public class Company extends RealmObject private String companyId private RealmList
  • 警报对话框中的 Webview 不显示内容

    我正在开发一个 Android 应用程序 我需要在网络视图和警报对话框上显示一个网站 该站点显示在网络视图中 但不显示在警报对话框中 到目前为止 这是我的代码 WebView WebView myWebView WebView v find
  • 通知声音不适用于 api 10 android

    我用这个功能来显示状态notification 一切正常 但没有声音播放notification public void notifiction main String ticker String title String text int
  • Android NDK 支持区域设置吗?

    我真正想做的就是使用格式化日期strftime x 以正确的顺序 在大多数平台上调用setlocale 足够 在 Android 上 我不断收到 美国日期 那么 Android 不支持语言环境吗 No setlocale and strft
  • 将人类日期(当地时间 GMT)转​​换为日期

    我正在服务器上工作 服务器正在向我发送 GMT 本地日期的日期 例如Fri Jun 22 09 29 29 NPT 2018在字符串格式上 我将其转换为日期 如下所示 SimpleDateFormat simpleDateFormat ne
  • 在 Xamarin 中隐藏软键盘

    如何隐藏软键盘以便在聚焦时显示Entry在 Xamarin forms 便携式表单项目中 我假设我们必须为此编写特定于平台的渲染器 但以下内容不起作用 我创建自己的条目子类 public class MyExtendedEntry Entr
  • PhoneGap 1.4 封装 Sencha Touch 2.X - 性能怎么样?

    我正在构建一个多平台平板电脑应用程序 仅使用其 Webview 使用 Phonegap 1 4 对其进行包装 然后使用 Sencha Touch 2 框架发挥我的魔力 我所说的多平台是指 iOS 5 X 和 Android 3 0 目前 到
  • Jetpack Compose 中复选框中的透明复选标记

    在我的 Compose 应用程序中 我需要创建一个圆形复选框 我已经通过下面的代码实现了这一点 Composable fun CircleCheckBox isChecked Boolean modifier Modifier Modifi
  • 蓝牙发送和接收文本数据

    我是 Android 开发新手 我想制作一个使用蓝牙发送和接收文本的应用程序 我得到了有关发送文本的所有内容逻辑工作 但是当我尝试在手机中测试它时 我看不到界面 这是Main Activity Code import android sup
  • Android 为什么这不会抛出错误的线程异常?

    我的印象是视图只能从主线程操作 但是 为什么这不会崩溃 public class MainActivity extends Activity TextView tv Override protected void onCreate Bund
  • Android Studio代理设置构建错误

    每当我尝试在 Android Studio 中构建应用程序时 都会收到以下错误 Error 169 254 16 169 254 16 Will ignore proxy settings for these hosts 我收到错误 5 次
  • Android View Canvas onDraw 未执行

    我目前正在开发一个自定义视图 它在画布上绘制一些图块 这些图块是从多个文件加载的 并将在需要时加载 它们将由 AsyncTask 加载 如果它们已经加载 它们只会被绘制在画布上 这工作正常 如果加载了这些图片 AsyncTask 就会触发v

随机推荐

  • 在 EMR 4.0 中启动 Spark 时出错

    我创建了一个EMR 4 0AWS 中的实例以及所有可用的应用程序 包括Spark 我通过 AWS 控制台手动完成此操作 我启动了集群并在启动时通过 SSH 连接到主节点 我跑到那里pyspark 当我收到以下错误时pyspark尝试创造Sp
  • JUnit5 测试未从“application-test.yml”加载属性

    我正在尝试为我的服务类编写单元测试 该服务类依赖于配置属性类 MyService java Service RequiredArgsConstructor public class MyService private final MyCon
  • jmeter-如何获取当前日期和时间(以秒为单位)

    我想计算当前时间 以秒为单位 并将其用作我的 jmeter 测试计划中的参数 默认情况下 时间以毫秒为单位 有人可以帮我吗 您可以使用 time 函数 http jmeter apache org usermanual functions
  • SQL 查询来计算不同值的数量

    x y A P A P B P B Q 你好 我需要一个查询来返回 x 的所有唯一值 有多少个不同的 y 因此 对于上述数据 它将返回 x count A 1 B 2 Thanks 使用 GROUP BY 和COUNT DISTINCT h
  • 使用 ndk-gdb 调试 Android 本机应用程序

    我正在尝试在 eclipse 中使用 ndk gdb 调试具有一些本机 c 代码的 android 应用程序 似乎 gdb 服务器从命令行成功启动并正在接受命令 但在 Eclipse 中却显示了这个错误 从设备获取文件时出错 com and
  • UCWA 或 UCMA API 是否支持 Skype for Business Online?

    我们正在尝试找出最新的UCWA https msdn microsoft com en us library office dn324971 v office 16 aspx or UCMA https msdn microsoft com
  • Elastic beanstalk需要python 3.5

    我最近使用最新的稳定版本的 python 3 5 创建了一个新的 python 程序 不幸的是 AWS EB 不提供 3 5 基础镜像 我一直在尝试配置 ebextensions获取图像来升级 python 发行版 这是第一个操作 我还没有
  • 插入 id(自动生成,仅列)

    如果我想在表中插入一行 而该表只包含一个具有自动生成 ID 的列 那么我的 SQL 语句 MS SQL 应该是什么样子 以下两个查询不起作用 INSERT INTO MyTable MyTableId VALUES Null or simp
  • 你能解释一下这个查询的逻辑吗

    我有一个查询 查找薪水第四高的老师的姓名 我不明白这部分 SELECT COUNT DISTINCT T2 salary FROM teacher as T2 WHERE T2 salary gt T1 salary 3 from SELE
  • PHP文件夹权限问题

    我正在尝试使用 PHP 创建一个文件夹 然后在其中创建另一个文件夹 如果这是我的目录结构 home site owner user1 现在 我使用创建文件夹 mkdir home site newdir 0777 user apache 目
  • Tkinter 标签文本在特定循环中重叠

    我正在使用 Python 和 Tkinter 开发一个 简单 搜索界面 这是我得到的一些示例代码 usr bin env python from tkinter import import os import csv import sys
  • 有没有办法捕获列表理解中的失误?

    基于简单的列表理解 yay i for i in a if a i nay i for i in a if not a i 我想知道是否有一种方法可以同时分配yay and nay一次值 即条件上的命中和未命中 看起来像这样的东西 yay
  • 了解 REST API - 什么是 Context 和 @Context?

    我最近学习了 Restful Web 服务教程 但无法理解什么是context是 有人可以解释一下它是什么以及什么吗 Context does JAX RS 提供 Context注解注入与 HTTP 请求上下文相关的 12 个对象实例 它们
  • 使用需要标头并提供内容的 REST 请求下载文件

    我正在使用带有 REST API 的 AngularJs 我不知道 REST API 我可以通过发送 REST 请求使用 API 存储数字对象 我也可以通过 GET 请求获取它 请求需要有一些特定的标头 我的目标是为用户提供 下载并另存为
  • 将 Laravel 5.4 升级到最新版本(5.7)

    我正在使用 PHP 版本 5 6 4 开发 Laravel 5 4 我的目标是将我的项目升级到 PHP 7 1 的 Laravel 5 7 现在我的问题是 我是否必须升级到 5 5 gt 5 6 gt 5 7 还是可以直接从 5 4 升级到
  • 为什么 TFS Power Tools 2013 安装不询问我是否要安装 PowerShell Cmdlet?

    我们有一个利用 TFS PowerShell 管理单元 Microsoft TeamFoundation PowerShell 的 PowerShell 脚本 在我的开发工作站上安装 TFS 2013 Power Tools 时 自定义安装
  • Pandas:分类列和每个类别的行插入

    我似乎无法实现插入缺少值的行 同时将一列作为分类 假设以下数据框 df 其中 B 列是分类的 类别应按 d b c a 的顺序出现 df pd DataFrame A i i i j k B pd Categorical d c b b a
  • ASP.Net 中线程敏捷性的含义是什么?

    我正在阅读一篇有关 HttpContext 和 CallContext 的文章并查看线程敏捷性 这是什么意思 这意味着 IIS 可以自由地使用多个线程来处理单个请求 尽管不是并行的 基本上 IIS 尝试异步执行 I O 操作 从而在操作期间
  • 如何遍历表单上的所有复选框?

    我有一个包含许多动态生成的复选框的表单 在运行时 我如何迭代它们中的每一个 以便获取它们的值和 ID foreach Control c in this Controls if c is CheckBox Do stuff here
  • 在 Android studio 中的自定义视图编辑器中显示正确的布局

    我在 Android Studio 中遇到布局预览的奇怪行为 我开发了一个扩展 AppCompatButton 的自定义视图 并且布局是运行时的预期布局 但是布局预览中的布局未正确显示 特别是 该按钮已显示 但没有从自定义属性设置背景颜色