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

时间:2013-09-22 02:32:23

标签: java android android-layout android-view android-inflate

所以,我正在读的是,根本不可能通过Java设置view的样式。目前还没有myView.setStyle("styleName")这样的方法。

然后,当您按代码创建布局元素(例如textvewsimageviewslinearlayouts作为容器时,如何设置其样式?是否按照每个新创建的元素的规则分配规则?或者有更有效的方法来完成这项任务吗?

@EDIT

好吧,我弄清楚它是怎么做到的。将使用

解决方案回答我的问题

4 个答案:

答案 0 :(得分:2)

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

来自文档

  

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

答案 1 :(得分:1)

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

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;
    }

}

答案 2 :(得分:1)

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

首先,我在 res / layout 文件夹中创建了一个新的XML文件。我将其命名为 template.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>

然后我在 styles.xml 文件中按照我想要的方式设置样式

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

    <style name="firstChild">
        <!-- style -->     
    </style>
</resources>

现在,在我的Activity类中,我添加了:

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

inflater将加载我们在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);
...

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

答案 3 :(得分:0)

您可以使用主题和样式

通过在Android清单

中指定主题的名称,可以将主题应用于整个应用程序

e.g。 <application android:theme="@style/CustomTheme">

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

有各种预定义的主题Holo Light和Holo Dark是比较常见的主题。大多数应用程序首先创建一个新主题,该主题继承自上述主题之一,并根据需要覆盖特定元素。

最好的办法是参考文档,这应该始终是您的第一个停靠点。

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