我一直在创建自定义视图一段时间,但从来没有以Android Studio预览正确显示它们的方式制作它们.
我使用以下命令初始化自定义属性:
if (!isInEditMode()) {
...
}
Run Code Online (Sandbox Code Playgroud)
但这会在预览中产生一个不可见的组件.关于如何实现这一点的任何想法?
谢谢.
android preview android-custom-view android-layout android-studio
在我的应用程序中,我经常依赖自定义构建视图,例如在以下示例中.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/light_grey"
android:layout_height="match_parent"
android:layout_width="fill_parent" >
<TextView
style="@style/CardTitle"
android:id="@+id/card_title"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<com.whiterabbit.cards.ui.AspectRatioImageView
android:id="@+id/card_picture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_marginLeft="30dip"
android:layout_marginRight="30dip"
android:src="@drawable/boss"
/>
<ListView
android:id="@+id/card_properties"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
Run Code Online (Sandbox Code Playgroud)
问题是,我不知道如果在真实设备或模拟器上运行它之前它是如何正确显示的.此外,如果我发现错误,我将不得不对其进行更改并再次部署应用程序以查看更改是否按预期工作.
这可能是一个漫长而枯燥的过程,特别是如果应用程序需要一些交互来访问您要检查的活动.
使用可视化编辑器不起作用,因为它无法加载自定义视图.
是否有另一种方法可以检查视图的显示方式而无需在整个应用程序中运行?
我创建了一个自定义 Button、TextView 和 ImageView。这些都没有在任何 XML 的图形布局中正确显示。它没有显示带有自定义字体的按钮或文本,而是显示一个带有我正在调用的自定义类名称的巨大灰色框。如何让这些显示在预览中?
public class FontTextView extends TextView {
public static Typeface FONT_NAME;
public FontTextView(Context context) {
super(context);
if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "font.ttf");
this.setTypeface(FONT_NAME);
}
public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "font.ttf");
this.setTypeface(FONT_NAME);
}
public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "font.ttf");
this.setTypeface(FONT_NAME);
}
Run Code Online (Sandbox Code Playgroud)
和
<com.example.gesturetest.FontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text placeholder" />
Run Code Online (Sandbox Code Playgroud)