【问题标题】:Set view style programmatically Kotlin以编程方式设置视图样式 Kotlin
【发布时间】:2020-03-12 19:38:22
【问题描述】:

我搜索了很多答案,但没有找到任何可行的解决方案。我想知道是否有可能以编程方式设置 Edittext 样式。在我的情况下,我有一个布局,它包含在两个不同的地方并在两个不同的地方使用,并且在包含它的地方旁边需要使用不同的样式。

<style name="TestStyle1" parent="Widget.AppCompat.EditText">
    <item name="android:maxLength">1</item>
    <item name="android:singleLine">true</item>
</style>

<style name="TestStyle2" parent="CodeTextStyle">
    <item name="android:alpha">0.5</item>
</style>

first_activity.xml

  <include
        layout="@layout/custom_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

second_activity.xml

  <include
        layout="@layout/custom_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

custom_layout.xml

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <CustomEditText
        android:id="@+id/et"
        app:style_ref="@style/CustomTextStyle"
        style="@style/TestStyle1" <-- Here i need to change style beside of usage
        android:layout_height="wrap_content" />
</LinearLayout>

CustomEditText.kt

class CustomEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : AppCompatEditText(context, attrs) {

init {
    val typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.CustomEditText, 0 , 0)
    try {

        val style2 = typedArray.getResourceId(R.styleable.CustomEditText_style_ref, 0)

        setTypeface(Typeface.DEFAULT, style2)
        invalidate()
    }
}

}

【问题讨论】:

  • 到目前为止,您是否尝试过任何 Kotlin 代码?这与数据绑定有什么关系?我看到的唯一选择是创建一个自定义 LinearLayout → CustomLayout 并将各自的样式属性放在构造函数中。
  • @nulldroid 抱歉,我错过了添加自定义类

标签: android kotlin styles themes android-custom-view


【解决方案1】:

因此,最简单的方法是,如果您的最低 API 级别为 23,则在相应活动布局膨胀后获取每个 CustomEditText 实例的引用,然后只需设置

myCustomEditText.setTextAppearance(R.style.TestStyle1)

this answer 中所述(正在使用已弃用的版本)

另一种选择是将这 2 种样式应用到两个各自的应用主题样式,您可以将它们设置到 AndroidManifest 中的每个活动。

或者你可以走我在评论中建议的更复杂的路径: 创建一个自定义的 LinearLayout 类并在构造函数中传递样式:

class CustomLayout(context: Context) : LinearLayout(context, null, R.style.TestStyle1)

您只需要想办法让布局知道它创建了哪个活动。据我所知,该样式随后将分别传递给每个子视图,即您的 CustomEditText。

【讨论】:

  • 如何在Button 上设置这个?
  • 按钮只是另一个具有文本功能的视图。也一样?
【解决方案2】:

两个有用的答案。

RelativeLayout layout = (RelativeLayout)getLayoutInflater().inflate(R.layout.template, null);

int buttonStyle = R.style.your_button_style;
Button button = new Button(new ContextThemeWrapper(context, buttonStyle), null, buttonStyle).

https://stackoverflow.com/a/24438579/5093308https://stackoverflow.com/a/5488652/5093308

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 2012-04-12
    • 2011-01-02
    • 1970-01-01
    相关资源
    最近更新 更多