【问题标题】:How to render both an xml view and a java view in android如何在android中同时呈现xml视图和java视图
【发布时间】:2013-05-31 09:27:19
【问题描述】:

在我的应用程序中,我有一个非常基本的指南针,它通过一个类在我的活动中呈现。我正在尝试使用布局显示指南针。因此,我可以包含文本框和按钮,而不是只有一个带有指向北方的线的圆圈。如何在布局中渲染它?目前我的活动设置内容视图如下:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        compassView = new CompassView(this);
        setContentView(compassView);

我试过setContentView(R.layout.activity_display_compass),这是我的xml文件,但它只显示"hello world"TextView),而不是指南针。请参阅下面的我的 xml 文件。

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>

    <View
        class = "com.example.gpsfinder.CompassView"
        android:id="@+id/compassView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

任何指导将不胜感激。

【问题讨论】:

    标签: java android xml class


    【解决方案1】:

    要在您的 xml 布局文件中使用自定义 View 子类:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"/>
    
        <com.example.gpsfinder.CompassView
            android:id="@+id/compassView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    

    然后你probably need to add some of these constructors in your Java code:

    // you used this ctor when creating the view programmatically
    public CompassView(Context context) {
        this(context, null);
        // add additional initialization here
    }
    
    // this constructor is needed for the class to be used in XML layout files!
    public CompassView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        // add additional initialization here
    }
    
    // this constructor is needed for the class to be used in XML layout files,
    //  with a class-specific base style
    public CompassView(Context context, AttributeSet attributeSet, int defStyle) {
        super(context, attributeSet, defStyle);
        // add additional initialization here
    }
    

    【讨论】:

    • 我说得太早了,我的 compassView 保持静止。即不根据方向移动?
    • 那将是一个单独的问题。我发布的代码是将自定义 View 子类集成到 Android XML 布局中所需的代码。我建议发布一个新问题,在其中解释指南针没有旋转。您需要展示您的指南针类的完整源代码(或至少大部分,包括处理旋转的部分)。
    • @Calgar99,不过,我只是想确保我理解你。您是说当您将设备平放时指南针不会旋转,并将其指向北、南、东、西?或者您是说整个CompassView 不旋转,例如当用户将手机从纵向转到横向时?这些是不同的问题,所以我想确保我清楚你在描述什么。谢谢。
    • 针不动,以前也是这样。
    • 好吧,我一开始以为你就是这个意思。正如我所说,这与仅在布局 xml 文件中使用自定义视图的问题无关。在没有看到 CompassView 代码的情况下,我唯一能想到的是我建议您添加更多构造函数(在这种情况下 Android 需要这些构造函数)。您是否记得在所有构造函数中都以相同的方式初始化视图?或者将它们链接在一起,以便无论使用哪个构造函数都运行相同的初始化代码?
    猜你喜欢
    • 2014-02-09
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多