Android 如何减少相同XML的数量,但仍能按预期方式运行?
我的应用程序有多个.xml,它们的设计和功能是相同的,唯一的区别是我将在每个设计中输入不同的数据,这意味着我的数据库中将有一堆表。我关心的是,既然我的xml的设计和功能是相同的,那么有没有什么方法可以使我的xml数量最小化,但仍然可以在数据库中存储不同的数据 这是我的主要活动,你可以想象它是什么样子。在每个按钮中,还有10个按钮将打开我将要创建的特定XMLAndroid 如何减少相同XML的数量,但仍能按预期方式运行?,android,xml,eclipse,Android,Xml,Eclipse,我的应用程序有多个.xml,它们的设计和功能是相同的,唯一的区别是我将在每个设计中输入不同的数据,这意味着我的数据库中将有一堆表。我关心的是,既然我的xml的设计和功能是相同的,那么有没有什么方法可以使我的xml数量最小化,但仍然可以在数据库中存储不同的数据 这是我的主要活动,你可以想象它是什么样子。在每个按钮中,还有10个按钮将打开我将要创建的特定XML package com.whowantstobeanengineer.systemproject; import android.supp
package com.whowantstobeanengineer.systemproject;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.widget.*;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//BUTTONS START//
Button btn_lvl_1 = (Button) findViewById(R.id.btn_lvl_1);
btn_lvl_1.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),level_1_subject_selection.class);
startActivity(i);
}
});
Button btn_lvl_2 = (Button) findViewById(R.id.btn_lvl_2);
btn_lvl_2.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),level_2_subject_selection.class);
startActivity(i);
}
});
Button btn_lvl_3 = (Button) findViewById(R.id.btn_lvl_3);
btn_lvl_3.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),level_3_subject_selection.class);
startActivity(i);
}
});
Button btn_lvl_4 = (Button) findViewById(R.id.btn_lvl_4);
btn_lvl_4.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),level_4_subject_selection.class);
startActivity(i);
}
});
Button btn_lvl_5 = (Button) findViewById(R.id.btn_lvl_5);
btn_lvl_5.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),level_5_subject_selection.class);
startActivity(i);
}
});
//BUTTONS END//
}
}
这里是主要的xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
tools:context="com.whowantstobeanengineer.systemproject.MainActivity"
tools:ignore="MergeRootFrame" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="20dp"
android:layout_weight="0.0"
android:text="@string/level_menu_text_view"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/btn_lvl_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_marginTop="20dp"
android:text="@string/level_1_text_button" />
<Button
android:id="@+id/btn_lvl_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_marginTop="20dp"
android:text="@string/level_2_text_button" />
<Button
android:id="@+id/btn_lvl_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_marginTop="20dp"
android:text="@string/level_3_text_button" />
<Button
android:id="@+id/btn_lvl_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_marginTop="20dp"
android:text="@string/level_4_text_button" />
<Button
android:id="@+id/btn_lvl_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_marginTop="20dp"
android:text="@string/level_5_text_button" />
</LinearLayout>
</ScrollView>
在leve_1_subject_select.xml下是这样的。你能想象我有5个级别(5个相同的XML)加上10个主题(每个级别10个相同的XML),这给了我50个XML来管理吗
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginLeft="125dp"
android:layout_marginTop="20dp"
android:layout_weight="0.0"
android:text="@string/level_1_menu_text_view"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/btn_lvl1_subj_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="20dp"
android:text="@string/subject_1_text_button" />
<Button
android:id="@+id/btn_lvl1_subj_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="20dp"
android:text="@string/subject_2_text_button" />
<Button
android:id="@+id/btn_lvl1_subj_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="20dp"
android:text="@string/subject_3_text_button" />
<Button
android:id="@+id/btn_lvl1_subj_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="20dp"
android:text="@string/subject_4_text_button" />
<Button
android:id="@+id/btn_lvl1_subj_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="20dp"
android:text="@string/subject_5_text_button" />
<Button
android:id="@+id/btn_lvl1_subj_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="20dp"
android:text="@string/subject_6_text_button" />
<Button
android:id="@+id/btn_lvl1_subj_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="20dp"
android:text="@string/subject_7_text_button" />
<Button
android:id="@+id/btn_lvl1_subj_8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="20dp"
android:text="@string/subject_8_text_button" />
<Button
android:id="@+id/btn_lvl1_subj_9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="20dp"
android:text="@string/subject_9_text_button" />
<Button
android:id="@+id/btn_lvl1_subj_10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="20dp"
android:text="@string/subject_10_text_button" />
</LinearLayout>
</ScrollView>
</LinearLayout>
我希望所有级别只能有至少1个xml,所有对象只能有1个xml,但仍然可以按我希望的那样工作。还有一件事,我的每个xml都有自己的类,所以最小化xml计数也会最小化我的类计数,这很好。我不知道是否可能,但我很高兴听到你们的建议。提前谢谢 删除两个
XML布局上的公共部分
,并将该UI部分保留在一个单独的布局文件中,必要时将其包含在主布局中
<include layout="layout id" />
创建与第一个相同但名称不同的新活动。对setContentView
中的活动使用相同的main_layout.xml文件,并确保活动中的代码使用与xml中相同的按钮名称。意思是说,先生,我将使用多个布局而不是多个xml?我不确定代码的外观。对不起,先生,我是一个noob。在您发布的代码中,只要在onCreate
中声明每个活动的setContentView
时重用xml,您也可以查看样式,它们将有助于减少每个元素中包含的xml量。