在 Android 上以编程方式创建的样式视图



所以伙计们,我读到,通过Java设置view的风格是不可能的。目前还没有这样的方法myView.setStyle("styleName")

然后,当您通过代码(例如textvews)创建布局元素并将imageviewslinearlayouts作为容器时,如何设置它们的样式?您是否为每个新创建的元素逐个规则分配?或者有没有更有效的方法来完成这项任务?

@EDIT

好吧,我想出了怎么做。将使用我正在使用的解决方案回答我的问题

每个视图或其子类都有第三个采用 Style 参数的构造函数。例如,视图的此构造函数。提及此视图的样式资源 ID,因此应在视图创建期间提及它。

从文档

要应用于此视图的默认样式。如果为 0,则不会应用任何样式(超出主题中包含的样式)。这可以是属性资源(其值将从当前主题中检索),也可以是显式样式资源。

您可以对要设置样式的视图进行子类化,并传递要在运行时应用的样式。类似于下面的类,它只是将自定义字体设置为 TextView。主要是,您需要查看可以提供样式的第三个构造函数。

public class TextViewRoboto extends TextView {
    public TextViewRoboto(Context context) {
        super(context);
    }
    public TextViewRoboto(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }
    public TextViewRoboto(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    }
    private void setCustomFont(Context ctx, AttributeSet attrs) {
        setCustomFont(ctx, "roboto-light.ttf");
    }
    public boolean setCustomFont(Context ctx, String asset) {
        Typeface tf = null;
        try {
            tf = Typefaces.get(ctx, asset);
        } catch (Exception e) {
            Logger.e("Could not get typeface: " + e.getMessage());
            return false;
        }
        setTypeface(tf);
        return true;
    }
}

我找到的解决方案 目标是以编程方式创建以前在其他地方设置样式的元素。

首先,我在 res/layout 文件夹中创建了一个新的 XML 文件。我将其命名为模板.xml并在其中插入了以下代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    style="@style/rootElement"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/firstChildId"
        style="@style/firstChild" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/secondChild" />
</LinearLayout>
然后我按照

我想要的方式在样式.xml文件中进行样式设置

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="rootElement">
        <!-- style -->          
    </style>
    <style name="firstChild">
        <!-- style -->     
    </style>
</resources>

现在,在我的活动课上,我添加了:

LinearLayout rootElement = (LinearLayout) getLayoutInflater().inflate(R.layout.template, null);
someOtherView.addView(rootElement);

inflower将加载我们在res/layout/template中创建的模板.xml(该文件中的所有元素及其属性),并将其分配给rootElement然后在我的代码中用于其他任何内容。例

TextView firstChild = (TextView) rootElement.getChildAt(0);
firstChild.setText("It is the firstChild element");

TextView firstChild = (TextView) rootElement.findViewById(R.id.firstChildId);
...

很简单,不是吗?!我希望这有所帮助

您可以使用

主题和样式

通过在 Android 清单中指定主题的名称,可以将主题应用于整个应用程序

例如 <application android:theme="@style/CustomTheme">

您可以通过指定特定样式来覆盖特定活动的主题。

有各种预定义的主题全息浅色和全息深色是更常见的主题。大多数应用首先创建一个继承自上述主题之一的新主题,并根据需要覆盖特定元素。

真正要做的最好的事情是参考文档,它应该始终是您的第一个停靠港。

http://developer.android.com/guide/topics/ui/themes.html

最新更新