簡體   English   中英

樣式化在Android上以編程方式創建的視圖

[英]Styling Views created programmatically on Android

所以我正在閱讀的家伙們說,根本不可能通過Java設置view的樣式。 尚無此類方法myView.setStyle("styleName")

然后,當您通過代碼創建布局元素(例如textvewsimageviewslinearlayouts作為容器)時,如何設置它們的樣式? 您是否按規則為每個新創建的元素分配規則? 還是有一種更有效的方法來執行此任務?

@編輯

好吧,我知道了怎么做。 用我正在使用的解決方案回答我的問題

每個View或其子類都有一個采用Style參數的第三個構造函數。 例如, View的構造函數。 提及此視圖的樣式資源ID,因此在創建視圖時應提及它。

從文檔

應用於此視圖的默認樣式。 如果為0,則不會應用任何樣式(超出主題中的樣式)。 這可以是屬性資源(其值將從當前主題中檢索),也可以是顯式樣式資源。

您可以將要設置樣式的視圖子類化,並在運行時傳遞希望應用的樣式。 類似於下面的類,它只是將自定義字體設置為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;
    }

}

我找到的解決方案目標是以編程方式創建以前在其他地方設置樣式的元素。

首先,我在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);

充氣器將加載我們在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">

您可以通過指定特定樣式來覆蓋特定活動的主題。

有各種預定義的主題,Holo Light和Holo Dark是較常見的主題。 大多數應用程序都從創建一個繼承自上述主題之一的新主題開始,並根據需要覆蓋特定元素。

真正最好的辦法是參考文檔,該文檔應該始終是您的第一站。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM