![](/img/trans.png)
[英]How to add layout.xml to another layout.xml programmatically
[英]one layout.xml for multiple activities
我是Java和Android的新手。 我正在制作一個應用程序(主要供我使用並獲得一些知識),其中我有一個列表視圖寬度101項。 當我單擊一個項目時,它會啟動一個具有不同布局的新活動。 101個項目中每個項目的布局相同,但內容不同。 我的問題是:我可以為所有項目制作一個.xml文件,但根據點擊的項目使內容不同?
我將在下面添加我的代碼(非常實驗性,並且肯定會改變布局):)謝謝你的答案!
MainAvtivity.java
public class MainActivity extends AppCompatActivity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
String[] values = new String[]{"§01. Kongle", "§02. Binders",
"§03. Plaster", "§04. Kvitering", "§05. 'Ruter'-logo", "§06. Ølkork", "§07. Hjerteutklipp av russekort",
"§08. Ølkork i gull", "§09. Bussbillett", "§10. Bit av et sitteunderlag", "§11. Signatur fra helsesøster",
"§12. Skolens logo", "§13. Første bokstav", "§14. Bit av skilt", "§15. Bit av burgereske", "§16. Fjær", "§16. Bilde av Barney Stinson",
"§17. Stoffbit", "§18. Champagnekork i gull", "§19. Billettarmbånd", "§20. Amnesty-logo", "§21. Plastskje", "§22. Mobildeksel",
"§24. Legokloss", "§25. Tampong", "§26. Kritt", "§27. Undertøy", //Plus more
};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_activated_2, android.R.id.text1, values);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
Intent myIntent = new Intent(view.getContext(), fastnokkel.class);
startActivityForResult(myIntent, 0);
}
if (position == 1) {
Intent myIntent = new Intent(view.getContext(), binders.class); //These classes will differ from witch item is clicked.
startActivityForResult(myIntent, 0);
}
if (position == 2) {
Intent myIntent = new Intent(view.getContext(), plaster.class);
startActivityForResult(myIntent, 0);
}
if (position == 3) {
Intent myIntent = new Intent(view.getContext(), plaster.class);
startActivityForResult(myIntent, 0);
}
if (position == 4) {
Intent myIntent = new Intent(view.getContext(), kongle.class);
startActivityForResult(myIntent, 0);
}
if (position == 5) {
Intent myIntent = new Intent(view.getContext(), binders.class);
startActivityForResult(myIntent, 0);
}
if (position == 6) {
Intent myIntent = new Intent(view.getContext(), kongle.class);
startActivityForResult(myIntent, 0);
}
if (position == 7) {
Intent myIntent = new Intent(view.getContext(), binders.class);
startActivityForResult(myIntent, 0);
}
}
});
}
Avtivity_Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:orientation="vertical"
tools:context="com.example.sande.listview8.MainActivity"
android:weightSum="1">
<ListView
android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
Same_Layout_Different_Content.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView2"
android:layout_width="150dp"
android:layout_height="150dp"
android:contentDescription="@string/bildeBesk"
app:srcCompat="@drawable/kongle"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/imageView2"
android:layout_centerHorizontal="true">
<TextView
android:id="@+id/gjenstand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="@string/kongle"
android:textSize="35sp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/gjoremaal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/gjenstand"
android:layout_marginTop="19dp"
android:textSize="30sp"
android:text="@string/kongleBeskrivelse"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
是的,將所有類型的視圖放在一個xml文件中。 就像在這個結構中:
<ParentLayout>
<Type1/>
<Type2/>
......
</ParentLayout>
保持所有類型布局的可見性為Gone。 然后,當在父活動中單擊項目時,傳遞唯一ID以標識布局類型。 然后在第二個活動中使用開關盒來激活必要的布局。
您需要做的是使用繼承BaseAdapter
的自定義適配器。
此適配器將能夠根據您的位置更新每個單元格的可視部分,並顯示/隱藏單個xml的元素。
例如:
public class MyAdapter extends BaseAdapter {
String[] values = new String[]{"§01. Kongle", "§02. Binders",
"§03. Plaster", "§04. Kvitering", "§05. 'Ruter'-logo", "§06. Ølkork", "§07. Hjerteutklipp av russekort",
"§08. Ølkork i gull", "§09. Bussbillett", "§10. Bit av et sitteunderlag", "§11. Signatur fra helsesøster",
"§12. Skolens logo", "§13. Første bokstav", "§14. Bit av skilt", "§15. Bit av burgereske", "§16. Fjær", "§16. Bilde av Barney Stinson",
"§17. Stoffbit", "§18. Champagnekork i gull", "§19. Billettarmbånd", "§20. Amnesty-logo", "§21. Plastskje", "§22. Mobildeksel",
"§24. Legokloss", "§25. Tampong", "§26. Kritt", "§27. Undertøy", //Plus more
};
@Override
public int getCount() {
return values.length;
}
@Override
public Object getItem(int i) {
return values[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (convertView == null) {
Context context = parent.getContext();
convertView = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
convertView.setTag(new ViewHolder());
}
// Here you set the view - set the text hide views etc... it would be beneficial to use ViewHolder pattern as well
ViewHolder holder = convertView.getTag();
holder.setData(i, values[i]);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.