java - Android:可以通过编程方式更改 View

标签 java android android-layout android-edittext

是否可以通过编程方式更改 View 外观顺序?

例子。

                <EditText
                    android:id="@+id/edit_txt_address_location_street"
                    android:layout_width="0dip"
                    android:layout_height="36dp"
                    android:layout_marginBottom="4dp"
                    android:layout_marginRight="4dp"
                    android:layout_marginTop="8dp"
                    android:layout_weight="8"
                    android:background="@drawable/shape_edit_text"
                    android:inputType="textNoSuggestions"
                    android:textColor="@color/black"
                    android:textCursorDrawable="@null" />

                <EditText
                    android:id="@+id/edit_txt_address_location_street_nr"
                    android:layout_width="0dip"
                    android:layout_height="36dp"
                    android:layout_marginBottom="4dp"
                    android:layout_marginLeft="4dp"
                    android:layout_marginRight="4dp"
                    android:layout_marginTop="8dp"
                    android:layout_weight="2"
                    android:background="@drawable/shape_edit_text"
                    android:inputType="textNoSuggestions"
                    android:textColor="@color/black"
                    android:textCursorDrawable="@null" />

关于显示正常外观顺序的xml。

1. edit_txt_address_location_street
2. edit_txt_address_location_street_nr

对于某些情况,我必须以编程方式反转外观顺序。 例如:

1. edit_txt_address_location_street_nr
2. edit_txt_address_location_street

最佳答案

大多数(如果不是全部的话)xml 命令都有其相应的编程调用。假设您的意思是“更改”是在用 xml 膨胀后添加或删除 View ,那么答案是肯定的,但这并不意味着您应该这样做。

如果您想动态添加/删除 View ,最好动态创建整个布局。相信我,一旦您知道如何操作,与 xml 布局相比并不难。我读过一些地方,如果你混合使用 xml 和动态布局可能会出现一些问题,我无法证明这一说法,但我所做的是用 xml 膨胀一个空的 linearlayout,然后动态添加所有 View 我要。

关于java - Android:可以通过编程方式更改 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17047274/

相关文章:

java - 为什么 Eclipse 3.2 在启动时抛出错误

java - 添加 Activity,布局显示但不起作用它是 java 代码(在 Activity 上)

android - Cordova 插件的输出延迟

java - setAlpha 以编程方式查看

java - 无法将管道分隔的 header 数据解析为正确的变量

java - 哪一种 AVD 目标最适合 Droid X2?

android - 使用 Jack 编译器时出错 - app/build/intermediates/packaged/debug/classes.zip' is an invalid library

android - 获取相对于android中最顶层父级的 View 位置

Android onfocuschange in xml

android - 如何在布局中向右和中间对齐高度