关于Android有很多组件共用的问题解决方法
2013-07-15 15:52
489 查看
如果一个项目有多个界面有一部分组件是共用的话,我们可以通过在父类中用一个布局把共用的组件写在一个布局文件中。
例: base_view.xml 父布局,将相同的布局放在父布局,也预留不同部分的布局来重写
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!-- 共用标题的组件 -->
<TextView
android:id="@+id/title_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<!-- 共用底部的组件 -->
<TextView
android:id="@+id/bottom_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/bottom_text" />
<!-- 中间不同的部分 -->
<LinearLayout
android:id="@+id/middle_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/bottom_text"
android:layout_below="@id/title_text"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
在父类中可以重写 onCreate()方法
package com.xwtec.demo;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class BaseActivity extends Activity
{
private LinearLayout middleLayout;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.setContentView(R.layout.base_view);
middleLayout = (LinearLayout)findViewById(R.id.middle_layout);
}
public void setContentView(int layoutId)
{
View middleView = getLayoutInflater().inflate(layoutId, null);
if (null != middleLayout)
{
middleLayout.removeAllViews();
middleLayout.addView(middleView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
}
}
子类的布局:view.xml 不同部分的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/title_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
子类MainActivity.java类
package com.xwtec.demo;
import android.os.Bundle;
//继承父类
public class MainActivity extends BaseActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.setContentView(R.layout.view);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xwtec.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.xwtec.demo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
例: base_view.xml 父布局,将相同的布局放在父布局,也预留不同部分的布局来重写
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!-- 共用标题的组件 -->
<TextView
android:id="@+id/title_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<!-- 共用底部的组件 -->
<TextView
android:id="@+id/bottom_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/bottom_text" />
<!-- 中间不同的部分 -->
<LinearLayout
android:id="@+id/middle_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/bottom_text"
android:layout_below="@id/title_text"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
在父类中可以重写 onCreate()方法
package com.xwtec.demo;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class BaseActivity extends Activity
{
private LinearLayout middleLayout;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.setContentView(R.layout.base_view);
middleLayout = (LinearLayout)findViewById(R.id.middle_layout);
}
public void setContentView(int layoutId)
{
View middleView = getLayoutInflater().inflate(layoutId, null);
if (null != middleLayout)
{
middleLayout.removeAllViews();
middleLayout.addView(middleView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
}
}
子类的布局:view.xml 不同部分的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/title_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
子类MainActivity.java类
package com.xwtec.demo;
import android.os.Bundle;
//继承父类
public class MainActivity extends BaseActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.setContentView(R.layout.view);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xwtec.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.xwtec.demo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
相关文章推荐
- 关于Mac系统下某些Android设备无法被adb识别问题的解决方法
- android 关于listview item设置高度的问题解决方法
- 关于Android开发导入已有项目遇到Project has no default.properties file! Edit the project properties to set one.问题的解决方法
- 关于android程序中使用bitmap放大功能时的OOM问题解决方法
- Android 关于ExpandableListView刷新问题的解决方法
- 关于图片资源在android2.1/2.2/2.3下无法显示的问题以及解决方法
- 关于多人共用一宽带账号上网网卡地址冲突问题解决方法
- 最近在ArcGIS Engine开发中关于调用gp工具过程出现COM 组件的调用返回了错误 HRESULT E_FAIL 错误的解决方法 和 学习oracle中遇到的一些问题总结
- 关于Android Logcat打印输出中文乱码问题的解决方法
- 关于Android长按出现复制粘贴栏在顶部占位问题解决方法
- 关于android-support-v7-appcompat.jar的使用的一些问题和解决方法
- 关于Android stdio使用的一些小问题的解决方法
- 关于 Android 5.0 原生系统网络图标上的感叹号问题解决方法
- 关于Android 4.4相机预览、录像花屏的问题的解决方法
- 【Android】关于Rendering Problems android.support.v7.internal.app.WindowsDecorActionBar问题的解决方法
- 关于Android SDK Manager启动时闪一下,就没反应的问题解决方法
- 关于Android DES加密算法在不同平台加密结果不同的问题的一个解决方法
- android 关于listview item设置高度的问题解决方法
- 关于Android原生集成5+webview,监听webview返回时,执行两次onkey方法问题的解决
- 关于java 和 android 下获取指定时间是本年第几周不一致问题的解决方法