- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我必须以编程方式执行此操作。所以,请耐心等待。
我将文本和两个按钮设置为在彼此下方对齐。因此,我为每个创建了一个新的 RelativeLayout.LayoutParams 实例,并添加了一条规则以将其设置在彼此之下。这行得通,我很高兴。
我的问题是:为什么我必须创建一个新实例才能让布局以这种方式显示它们?有什么方法可以减少以下代码,使其不那么重复(除了编写我自己的私有(private)方法来干掉它之外)。我想知道是否有一种方法可以利用我已经使用的东西在更少的代码行中完成同样的事情可能在 Android SDK 中被忽略了)?我是否必须继续为每个元素创建一个新的 RelativeLayout.LayoutParams 实例?
layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
text = new TextView(this);
text.setId(1);
text.setText("This is text");
layout.addView(text);
myButton1 = new Button(this);
myButton1.setId(2)
myButton1.setOnClickListener(this);
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.BELOW, text.getId());
layout.addView(myButton1, buttonParams);
myButton2 = new Button(this);
myButton2.setId(3)
myButton2.setOnClickListener(this);
buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.BELOW, myButton1.getId());
layout.addView(myButton2, buttonParams);
最佳答案
不幸的是,RelativeLayout.LayoutParams
没有removeRule()
方法(至少根据API )所以你必须创建一个新的 LayoutParams
对象每次。
我倾向于同意@jeffamaphone 的观点,即您应该更喜欢 xml 布局而不是以编程方式设置布局。即使您没有将整个布局放在 xml 中,您也应该使用资源 ID(有关详细信息,请参阅 doc)来设置项目的 ID。这将保证唯一的 ID。
关于android - 为什么必须为放入 View 中的每个元素创建一组新的 RelativeLayout.LayoutParams?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7534317/
我试图在 2 个小部件/布局之间创建一个 float 操作按钮 我 99% 确定我已经使用了这个教程并且有效 How can I add the new "Floating Action Button
这是 XML 代码,
因为它可以与Relative 或Linear 布局一起使用。因此,如果我们稍后将 RelativeLayout 更改为 LinearLayout,它可能会很有用。 LinearLayout.Layou
我正在尝试开发 Android 3.1 平板电脑应用程序。 这个app会有很多图片,我已经关注了Processing Bitmaps Off the UI Thread教程,但我做错了,因为我得到:
我的初始代码行位于重写方法 onGlobalLayout() 中: img.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 10
我正尝试按照发布在以下位置的示例编写周历: How can I create a weekly calendar view for an Android Honeycomb application?
这段代码似乎适用于 android api 16 - 22,但不适用于 api 23。我只是想显示一个带有选项的弹出窗口,并使弹出窗口下方的背景变暗: WindowPo
带有 Android Studio 2.0 的 OS X Yosemite 10.10.5 出于教学目的,我想将尽可能多的 View 属性从 XML 移动到 Java。 我在 GridLayout 中
说明及解决方法在底部。 我正在开发一个 slider 布局动画,动画效果很好,但是当所有进程结束时,它们会得到下一个异常。 我猜 RelativeLayout 父级在异常中有一些事情要做,但我不知道如
我正在尝试创建一个 FloatingActionButton,如 this , 但是当我运行应用程序时出现以下错误 java.lang.ClassCastException: android.wid
根据大量教程和示例项目,我得到了以下代码,它应该用包含来自 Line 对象的多个值的行填充 ListView。 作为一名新手程序员,我一辈子都弄不明白我的代码到底哪里出错了。任何帮助将不胜感激! 如果
我得到了这个异常(exception): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cas
Android Studio 3.1、Java 1.8、Gradle 4.1。我使用 minVersion = 15 我想在文件 profile_category_active.xml 中以编程方式设
我正在尝试使我的整个布局(其中包含 ListView)可滚动。也就是说,整个布局应该拥有滚动条,而不仅仅是 ListView。我正在尝试使用 commonsguy 的 MergeAdapter ( h
我遇到了以下错误 java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to an
还有其他相关问题,但它们没有解决我的情况(它们的错误代码与客户端代码的某种回收和/或错误的转换调用有关)。 我的情况比较复杂。 我有一些代码,用户可以在其中调出照片库。 问题是,它在我拥有的 7 部手
我正在尝试做抽屉导航选项。 这是我的 xml: 如果我删除 Linearlayout
我正在尝试获取 AppBarLayout 的布局参数,但出现此错误。 java.lang.ClassCastException: android.support.design.widget.Coord
我使用一个具有RelativeLayout -> ScrollView -> LinearLayout 的 View 。我得到了异常(exception): 11-22 11:17:24.605: E
我在调试我正在处理的 Android 应用程序时遇到了问题。目前,它应该能够从文本文件中获取输入(它可以),将其转换为包含表示网格部分的 Cell 对象的 ArrayList(它确实如此),然后使用
我是一名优秀的程序员,十分优秀!