Android应用开发的一些规范
1.资源定义规范:
1). 对于用户界面中的文本,务必将每个字符串都指定为资源。
所有视图的默认 weight 值都为 0,所以如果您仅将一个视图的 weight 值指定为大于 0,那么等到其他所有视图都获得所需空间后,该视图便会填满所有剩余空间.
将宽度设置为零 (0dp) 可提高布局性能,这是因为如果将宽度设置为
3).数据传递
Intent对象提供putExtra()与getExtra()可以完成页面跳转的数据传递。,在目标Activity通过getIntent获取intent对象。
4).国际化,只针对界面显示的文本,做对应的国际化
eg: 西班牙语,
5)支持多种屏幕的布局定义
1).定义fragment子类extends Fragment类
1). 对于用户界面中的文本,务必将每个字符串都指定为资源。
<string name="button_send">发送</string>2 ).weight
所有视图的默认 weight 值都为 0,所以如果您仅将一个视图的 weight 值指定为大于 0,那么等到其他所有视图都获得所需空间后,该视图便会填满所有剩余空间.
将宽度设置为零 (0dp) 可提高布局性能,这是因为如果将宽度设置为
"wrap_content"
,则会要求系统计算宽度,而该计算最终毫无意义,因为 weight 值还需要计算另一个宽度,才能填满剩余空间
3).数据传递
Intent对象提供putExtra()与getExtra()可以完成页面跳转的数据传递。,在目标Activity通过getIntent获取intent对象。
4).国际化,只针对界面显示的文本,做对应的国际化
eg: 西班牙语,
/values-es/strings.xml
:5)支持多种屏幕的布局定义
res/ layout/ # default (portrait) main.xml layout-land/ # landscape main.xml layout-large/ # large (portrait) main.xml layout-large-land/ # large landscape main.xml 6).支持不同的平台版本(4-15)二.fragment构建界面<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... > <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" /> </manifest>7).运用平台自带的样式和主题<activity android:theme="@android:style/Theme.Dialog">
应用自定义风格 /res/values/styles.xml
:<activity android:theme="@style/CustomTheme">应用整个应用所有activity的主题
<application android:theme="@style/CustomTheme">
1).定义fragment子类extends Fragment类
public class ArticleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.article_view, container, false); } }2 ).定义fragment的布局文件
<fragment android:name="com.example.android.fragments.HeadlinesFragment" android:id="@+id/headlines_fragment" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" />3 ).在Activity的xml中引入fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment android:name="com.example.android.fragments.ArticleFragment" android:id="@+id/article_fragment" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>4.在Activity中添加fragment
public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); if (findViewById(R.id.fragment_container) != null) { if (savedInstanceState != null) { return; } // Create a new Fragment to be placed in the activity layout HeadlinesFragment firstFragment = new HeadlinesFragment(); // In case this activity was started with special instructions from an // Intent, pass the Intent's extras to the fragment as arguments firstFragment.setArguments(getIntent().getExtras()); // Add the fragment to the 'fragment_container' FrameLayout getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit(); } } }5.更换布局中的fragment
ArticleFragment newFragment = new ArticleFragment(); Bundle args = new Bundle(); args.putInt(ArticleFragment.ARG_POSITION, position); newFragment.setArguments(args); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack so the user can navigate back transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit();