【问题标题】:Custom component is not showing in Edit Mode on Android Studio自定义组件未在 Android Studio 的编辑模式下显示
【发布时间】:2015-05-26 14:15:24
【问题描述】:

我在 Android Studio 中创建了一个自定义组件,它没有在编辑模式下呈现,但它在我在模拟器中运行时显示。 我在一个名为 custom 的模块中创建了 sn-pLabel.java。

public class SnippetLabel extends LinearLayout {

    TextView label;
    TextView content;

    public SnippetLabel(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.setOrientation(VERTICAL);

        LayoutParams labelLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        labelLayoutParams.setMargins(0, 0, 0, (int) context.getResources().getDimension(R.dimen.snippetabel_label) );

        LayoutParams contentLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);


        label = new TextView(context, attrs);
        label.setTextAppearance(context, R.style.GridSystem_Forms_SnippetLabelLabel);
        addView(label, labelLayoutParams);

        content = new TextView(context, attrs);
        content.setTextAppearance(context, R.style.GridSystem_Forms_SnippetLabelContent);
        addView(content, contentLayoutParams);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.snippet_label);
        CharSequence sl = a.getString(R.styleable.snippet_label_label);
        CharSequence sc = a.getString(R.styleable.snippet_label_content);
        if (sl != null) { label.setText(sl); }
        if (sc != null) { content.setText(sc); }
        a.recycle();
    }

    public void setContentText(String s){
        content.setText(s);
    }

    public void setLabelText(String s){
        label.setText(s);
    }

    public String getContentText(){
        return (String) content.getText();
    }



}

SnippetLabel.java

<resources>
    <declare-styleable name="snippet_label">
        <attr name="label" format="string" />
        <attr name="content" format="string" />
    </declare-styleable>
</resources>

属性.xml

<style name="GridSystem.Forms.SnippetLabel">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">wrap_content</item>
</style>

<style name="GridSystem.Forms.SnippetLabelLabel">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_marginBottom">@dimen/snippetabel_label</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">@color/SnippetLabelLabel</item>
    <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
</style>

<style name="GridSystem.Forms.SnippetLabelContent">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">@color/SnippetLabelContent</item>
    <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
</style>

style.xml

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/currentRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">


<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">


        <LinearLayout
            android:id="@+id/linearLayoutForm"
            style="@style/GridSystem.Panel">

            <TextView
                style="@style/GridSystem.Title"
                android:text="Resultado" />

            <LinearLayout
                android:id="@+id/secao_entregue_na_grafica"
                style="@style/GridSystem.Row">

                <view
                    android:id="@+id/view"
                    class="br.gov.prodemge.comum.ui.snippets.SnippetLabel"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="100" />

                <br.gov.prodemge.comum.ui.snippets.SnippetLabel
                    android:id="@+id/campo_entregue_na_grafica"
                    style="@style/GridSystem.Forms.SnippetLabel"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="100"
                    app:content="#"
                    app:label="Entregue na Gráfica:" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/secao_prazo"
                style="@style/GridSystem.Row">

                <br.gov.prodemge.comum.ui.snippets.SnippetLabel
                    android:id="@+id/campo_situacao"
                    style="@style/GridSystem.Forms.SnippetLabel"
                    android:layout_weight="50"
                    app:content="#"
                    app:label="Situação:" />

                <br.gov.prodemge.comum.ui.snippets.SnippetLabel
                    android:id="@+id/campo_prazo"
                    style="@style/GridSystem.Forms.SnippetLabel"
                    android:layout_weight="50"
                    app:content="#"
                    app:label="Prazo:" />


            </LinearLayout>

            <LinearLayout
                android:id="@+id/secao_mensagem_status"
                style="@style/GridSystem.Row">

                <br.gov.prodemge.comum.ui.snippets.SnippetLabel
                    android:id="@+id/campo_mensagem_status"
                    style="@style/GridSystem.Forms.SnippetLabel"
                    android:layout_weight="100"
                    app:content="#"
                    app:label="[Mensagem de Status]:" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/secao_ar_correio"
                style="@style/GridSystem.Row">

                <br.gov.prodemge.comum.ui.snippets.SnippetLabel
                    android:id="@+id/campo_ar_correio"
                    style="@style/GridSystem.Forms.SnippetLabel"
                    android:layout_weight="100"
                    app:content="#"
                    app:label="AR Correio:" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/secao_situacao_entrega"
                style="@style/GridSystem.Row">

                <br.gov.prodemge.comum.ui.snippets.SnippetLabel
                    android:id="@+id/campo_situacao_entrega"
                    style="@style/GridSystem.Forms.SnippetLabel"
                    android:layout_weight="100"
                    app:content="#"
                    app:label="Situação da Entrega:" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/secao_motivo_rejeicao"
                style="@style/GridSystem.Row">

                <br.gov.prodemge.comum.ui.snippets.SnippetLabel
                    android:id="@+id/campo_motivo_rejeicao"
                    style="@style/GridSystem.Forms.SnippetLabel"
                    android:layout_weight="100"
                    app:content="#"
                    app:label="Motivo Rejeição:" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/secao_acao"
                style="@style/GridSystem.Row">

                <br.gov.prodemge.comum.ui.snippets.SnippetLabel
                    android:id="@+id/campo_acao"
                    style="@style/GridSystem.Forms.SnippetLabel"
                    android:layout_weight="100"
                    app:content="#"
                    app:label="Ação:" />

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</ScrollView>

layout.xml

【问题讨论】:

  • 很棒的帖子,感谢您提供的详细信息,但您的问题是什么?

标签: java android xml android-layout


【解决方案1】:

您的自定义视图类必须实现更多构造函数,视图在预览和设备/模拟器上的实例化方式不同。一种常见的模式是将您的初始化代码移动到某个方法并从构造函数中调用它。看例子

public class SnippetLabel extends LinearLayout {
    public SnippetLabel(Context context) {
        super(context);
        init();
    }

    public SnippetLabel(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SnippetLabel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        this.setOrientation(VERTICAL);
        ...
    }
}

【讨论】:

  • 工作!非常感谢!
【解决方案2】:

还有一件事要记住,补充@Lamorak 的答案: 如果您的组件依赖于外部数据,例如来自服务的状态、来自共享首选项的数据等,您必须使用 try-catch 覆盖您的 init() 方法。

在设计模式下,您无权访问外部数据,因此任何阻止构造函数完成的问题都不会显示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    相关资源
    最近更新 更多