在两个单独的布局文件中引用 TextView @id?

发布于 2024-11-17 23:19:47 字数 1228 浏览 5 评论 0原文

有没有办法在单独的布局上创建 TextView 实例?我创建了一个包含 x 个对象的 ExpandableListView。我还想展示折叠组中的一个孩子。我意识到我可以在代码中进行设置,但是如果我可以多次指向相同的 id R.id.dscItem ,则会更容易、更清晰。

我尝试将“@+id/dscItem”中的 + 删除,使其成为“@id/dscItem”,但效果不大。难道我只能指向这个id一次吗!?

SimpleExpandableListAdapter mAdapter = new SimpleExpandableListAdapter(
            this, createGroup(), R.layout.exprow, R.layout.row,
            new String[] { Items.ITEMS_ITEM }, new int[] { R.id.txtItem },
            createChildren(), R.layout.exprow, new String[] {
                    Items.ITEMS_DESC, Items.ITEMS_MANU },
            new int[] { R.id.dscItem, R.id.manuItem });

XML 布局 1:

></TextView>
<TextView 
android:text="Description" 
android:id="@id/dscItem"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:textSize="15dip"

></TextView>

XML 布局 2

<TextView 
android:text="Description" 
android:id="@+id/dscItem" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:textColor="#994020"
android:layout_gravity="right"

></TextView>

Is there a way to create an instance of a TextView on a separate layout? I have created an ExpandableListView with x number of objects. I would like to display one of the children in the collapsed group as well. I realize I could set up in the code, however it would be much easier, and much cleaner if I could just point to the same id R.id.dscItem multiple times.

I have tried dropping the + in "@+id/dscItem"so that it was "@id/dscItem" but it didn't do much. Is it true that I can only point to this id once!?

SimpleExpandableListAdapter mAdapter = new SimpleExpandableListAdapter(
            this, createGroup(), R.layout.exprow, R.layout.row,
            new String[] { Items.ITEMS_ITEM }, new int[] { R.id.txtItem },
            createChildren(), R.layout.exprow, new String[] {
                    Items.ITEMS_DESC, Items.ITEMS_MANU },
            new int[] { R.id.dscItem, R.id.manuItem });

XML layout 1:

></TextView>
<TextView 
android:text="Description" 
android:id="@id/dscItem"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:textSize="15dip"

></TextView>

XML layout 2

<TextView 
android:text="Description" 
android:id="@+id/dscItem" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:textColor="#994020"
android:layout_gravity="right"

></TextView>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

套路撩心 2024-11-24 23:19:47

您可以在单独的布局中创建一个文本视图,并通过 xml 中的布局合并或 java 代码中的布局 inflater 来使用它
http://developer.android.com/resources/articles/layout-tricks -merge.html

You can create a textview in a separate layout and use it by layout merge in xml or layout inflater in java code
http://developer.android.com/resources/articles/layout-tricks-merge.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文