我在Android Studio中创建了一个自定义组件,但它没有在编辑模式下渲染,但它在我在模拟器中运行时显示。 我在名为custom的模块中创建了snippetLabel.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>
Attrs.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
答案 0 :(得分:0)
您的自定义视图类必须实现更多的构造函数,视图在预览版和设备/模拟器中的实例化方式不同。一种常见的模式是将初始化代码移动到某个方法并从construstors中调用它。见例子
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);
...
}
}
答案 1 :(得分:0)
还有一点要记住,补充@Lamorak的回答:
如果您的组件依赖于外部数据,例如来自服务的状态、来自共享首选项的数据等,您必须使用 try-catch 来包围您的 product.info
方法。
在设计模式下,您无法访问外部数据,因此任何阻止构造函数完成的问题,您的组件都不会显示。