哪种方式更好地创建android视图?

问题描述 投票:0回答:2

哪种方式更好地创建android视图?通过使用xml或bay使用java代码进入活动?

示例:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/account_bundle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:background="@drawable/account"
    android:orientation="vertical"
    android:paddingBottom="10dp">


    <LinearLayout
        android:id="@+id/progress_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal">

        <ProgressBar
            android:id="@+id/account_progress_bar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_height="5dp"
            android:layout_width="200dp"
            android:paddingEnd="10dp"
            android:layout_gravity="center|end"
            android:layout_weight="4"
            android:indeterminate="false"
            android:max="100"
            android:progress="80"
            android:progressDrawable="@drawable/progress_bar"
            android:textAlignment="center" />

    </LinearLayout>

</LinearLayout>

我可以选择用xml和java创建这个linearLayout到我的活动中,我想现在哪个是性能方面的最佳解决方案?

android android-activity layout
2个回答
1
投票

Android框架为应用程序开发人员提供了使用灵活性1. XML声明和2.在运行时实例化布局元素,用于声明和管理应用程序的UI

使用XML方法的基本原理是,它使您能够更好地将应用程序的表示与控制其行为的代码分开。 XML中的UI描述是应用程序代码的外部描述,这意味着您可以修改它,而无需修改应用程序的源代码并重新编译。

Android documentation没有提到一种方法相对于另一种方法的任何性能优势。

但是,在XML中声明布局可以更容易地可视化UI的结构,因此,由于UI分离为xml,因此更容易调整UI中的问题/问题。


3
投票

如果要创建静态TextView(颜色,文本大小,父宽度等[例如]),请将所有内容保存在XML中。如果您根据应用程序的特定信息更改它,那么它是否在XML中并不重要,因为无论如何您将在代码内部更改它。

性能方面有一种方式比另一方面好吗?没有。

我喜欢干净的代码,所以我尽量不对我的课程中的项目进行很多更改。

© www.soinside.com 2019 - 2024. All rights reserved.