Android 以编程方式更改按钮的样式值

Android 以编程方式更改按钮的样式值,android,android-layout,android-studio,Android,Android Layout,Android Studio,我为一个按钮创建了以下设计 利用 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:right="245dp"> <shape android:shape="rectangle" > <corners android:radius="3dp" /> <

我为一个按钮创建了以下设计

利用

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:right="245dp">
        <shape android:shape="rectangle" >
            <corners android:radius="3dp" />
            <solid android:color="#000" />
        </shape>
    </item>

    <item android:left="0dp">
        <shape android:shape="rectangle" >
            <corners android:radius="3dp" />
            <solid android:color="#80000000" />
        </shape>
    </item>
</layer-list>`

`
我想要实现的是:

一旦要打开的片段完成(一个地址),则+符号应该在一个“勾号”中改变。如何降低黑色矩形的不透明度?我还没有弄明白如何做+符号,所以欢迎任何想法

主要的问题是:如何在android中使用代码设置这些样式值?我想将第一个项目(245dp)的大小更改为按钮宽度的5%

我的按钮布局:

<Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_marginTopPercent="7%"
        app:layout_marginBottomPercent="6%"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        app:layout_widthPercent="50%"
        android:layout_below="@id/button7"
        android:background="@drawable/btnstyle"
        android:text="Create"
        android:textColor="#fff" />


btnstyle的定义如上所述

假设背景是background.xml放在drawables文件夹中。 你可以用这个-

<LinearLayout android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="100dp"
                android:background="@drawable/background"
                android:orientation="horizontal">
    <ToggleButton
      style="?android:attr/borderlessButtonStyle"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginBottom="10dp"
      android:layout_marginLeft="20dp"
      android:layout_marginStart="20dp"
      android:layout_marginTop="10dp"
      android:checked="false"
      android:drawablePadding="80dp"
      android:drawableRight="@drawable/button_image"
      android:textColor="@android:color/white"
      android:textOff="Button1"
      android:textOn="Button1"/>

  </LinearLayout>

对于可绘制的按钮\u image.xml,以下是代码-

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="false" android:state_focused="true" android:drawable="@drawable/plus_img"/>
  <item android:state_checked="false" android:state_focused="false" android:drawable="@drawable/plus_img"/>
  <item android:state_checked="true" android:state_focused="true" android:drawable="@drawable/tick_img"/>
  <item android:state_checked="true" android:state_focused="false" android:drawable="@drawable/tick_img"/>
</selector>

在toggleButton的clickListener上,您可以打开toggleButton的片段并将其设置为!toggleButton.isChecked()。基本上是这样的-toggleButton.setChecked(!toggleButton.isChecked())

关于不透明度
在您编写的xml中,矩形中第一个项目的颜色是#RGB格式。它也可以是ARGB,其中A代表alpha。因此,如果设置A=0,则视图将是透明的,如果A=f,则视图将是不透明的。

您可以参考此内容,很抱歉响应太晚,但我刚刚尝试了您的布局,我有一些问题。我还更新了按钮的布局。我的主要问题是关于那个小矩形,它是按钮的一部分,而不是它的不透明度。基本上我希望按钮宽度的5%是一种颜色,其余的是另一种颜色。以及切换按钮内的图标,如何更改其大小。现在我想了想,难道我不能做一个完整的布局,让它可以点击吗?或者这是一个糟糕的方法吗?我可以很容易地通过使用百分比来实现布局的外观。使用布局肯定会更灵活。您可以控制其中的一切,从大小到分配的空间百分比。因此,使用权重,您可以实现5%的标记:)此外,就可绘制的切换按钮的大小而言,我认为您无法将其操纵到某个特定大小之外。所以,如果可以,你应该使用布局。另外,如果您希望您的布局具有选中的功能,请使用此-。然后您可以为其所有子项duplicateparentstate=“true”并使其可单击=“false”。谢谢。我会调查的。自去年以来,有一个新的百分比库,在我看来,这比使用权重要好得多。它给你更多的灵活性。至于可点击状态,我真的不在乎,因为我想让用户修改内容,如果他愿意的话。所以使用可点击的布局并没有错,对吧?就我所知,你能解释一下什么时候可能是错的吗?