简体   繁体   English

如何预览自定义android组件布局?

[英]How can I preview a custom android component layout?

I've created a custom view which extends RelativeLayout , and I want to preview how it looks in the designer. 我创建了一个自定义视图,该视图扩展了RelativeLayout ,并且我想预览它在设计器中的外观。

The java is something like this: Java是这样的:

// The view for a snap in the search/browse fragment.
public class MyView extends RelativeLayout
{
    public MyView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.the_layout, this);
    }

    public void setData(String text)
    {
        mText.setText(text);
    }

    // *** Views ***
    private TextView mText;

    @Override
    protected void onFinishInflate()
    {
        super.onFinishInflate();

        mText = (TextView)findViewById(R.id.text);
    }
}

And the XML is like this: XML是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!-- (And lots more views) -->
</RelativeLayout>

There are a few problems with this however: 但是有一些问题:

  1. This actually creates a RelativeLayout within a RelativeLayout which is pointless. 实际上,这在毫无意义的RelativeLayout创建了RelativeLayout It can be solved by changing the XML <RelativeLayout> to a <merge> but then I can't preview the layout at all! 可以通过将XML <RelativeLayout>更改为<merge>来解决,但是我根本无法预览布局!
  2. I want to use the isInEditor() function (or whatever it is called) in the java to add some sample data for previewing purposes, but I can't find a way to tell the XML editor that it should display a preview of my class instead of the actual XML. 我想在isInEditor()使用isInEditor()函数(或任何所谓的函数)添加一些示例数据以供预览之用,但是我找不到一种方法来告诉XML编辑器它应该显示我的类的预览而不是实际的XML。

One unsatisfying solution I can think of is to create an empty preview.xml file with only <com.foo.bar.MyView/> in it... But that seems kind of silly. 我可以想到的一个令人不满意的解决方案是创建一个仅包含<com.foo.bar.MyView/>的空的preview.xml文件。但这似乎有点愚蠢。 Is there a better way? 有没有更好的办法? (I don't really care about editing as much as previewing, since - let's face it - Eclipse/ADT are way too slow and flaky to make graphical layout editing usable.) (我并不真正关心编辑,而是预览,因为-让我们面对现实-Eclipse / ADT太慢太笨拙,无法使用图形布局编辑。)

If I understand your problem correctly I would say that the solution is to just replace the "RelativeLayout" tags (or any other tag for that matter) in your xml layout with "your.packagename.MyView" as in: 如果我正确理解了您的问题,我会说解决方案是将XML布局中的“ RelativeLayout”标签(或与此相关的任何其他标签)替换为“ your.packagename.MyView”,如下所示:

<your.packagename.MyView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

  <!-- (And lots more views) -->

</your.packagename.MyView>

If you'll get any exceptions regarding MyView class while running your app, add all the missing super/parent constructors. 如果在运行应用程序时遇到有关MyView类的任何异常,请添加所有缺少的超级/父构造函数。

I do this for almost all of my custom xml layouts. 我对几乎所有的自定义xml布局都执行此操作。 Extending your class with RelativeLayout, LinearLayout or any other GUI class also gives you great controll over how your GUI should behave (because you can also override parent methods etc.). 使用RelativeLayout,LinearLayout或任何其他GUI类扩展您的类,还可以使您很好地控制GUI的行为(因为您还可以覆盖父方法等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM