【问题标题】:there is some way to make my own item in xml?有什么方法可以在 xml 中制作我自己的项目?
【发布时间】:2018-06-25 23:30:28
【问题描述】:
我从事 android-studio 项目,我的问题是关于
我可以通过某种方式在 xml 类型中定义一些包含
很少编辑文本和按钮,并打开包含我创建的项目的列表视图?
有点像:
<item
<Editext(some setting)/>
<Edittext 1(some setting)/>
<Button(some setting)/>
/>
然后是一些可以调整的适配器或类似的东西,我可以添加到 ListView。
我在 youtube 上看到了一些试图解释这一点的视频,但我有库存.. 我真的不明白。
【问题讨论】:
标签:
java
android
listview
android-studio
android-xml
【解决方案1】:
我花了一段时间才弄清楚,但它确实有效。
随心所欲地创建布局文件。另存为res/layout/something.txt。示例:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:text="hello"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:id="@+id/text1"/>
<TextView
android:text="world"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:id="@+id/text2"/>
</LinearLayout>
在你的代码中使用如下:
LinearLayout mainLinearLayout = new LinearLayout(getApplicationContext());
// inflate your container
View view = inflater.inflate(R.layout.something, null);
// you can make changes to the elements like this:
TextView txt1 = (TextView) view.findViewById(R.id.text1);
TextView txt2 = (TextView) view.findViewById(R.id.text2);
view.setTag(tag);
// add to main layout
mainLinearLayout.addView(view);
其中tag 是用于标识视图的唯一整数。如果只使用一次容器,则无需设置标签。
我做这个的时候没有使用适配器。我只是使用ScrollView 来制作一个连续的条目列表。根据您的需要进行更改。