解决Android中顶部title功能重复问题,减少代码冗余的功能
2016-11-02 12:46
344 查看
在开发中我之前遇到顶部Title的功能重复,但是每个页面都需要我自己去获取按钮,然后点击监听,我就在想有没有什么方法可以一次性的解决这个问题,后来就有了下文。
直接上源代码吧!
首先是公用的title的布局文件title.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="wrap_content">
<Button
android:id="@+id/title_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:text="Back"
android:textColor="#fff" />
<TextView
android:id="@+id/title_text"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Title Text"
android:textColor="#ff22ff"
android:textSize="24sp" />
<Button
android:id="@+id/title_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:text="Edit"
android:textColor="#fff" />
</LinearLayout>
然后是写一个类用来完成需要完成的功能,比如返回到上一个界面,或者是显示其他的什么,都是可以的,类名TitleLayout.java:
code:
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class TitleLayout extends LinearLayout{
private Button backButton;
private Button editButton;
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);
backButton = (Button) findViewById(R.id.title_back);
editButton = (Button) findViewById(R.id.title_edit);
backButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
((Activity)getContext()).finish();
}
});
editButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getContext(), "you click the edit button", Toast.LENGTH_LONG).show();
}
});
}
}
随后是在需要的页面进行调用,其实也就是一个自定义的类。在MainActivity当中调用。就是在布局文件当中调用activity_main.xml:
code:
<LinearLayout 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"
android:orientation="vertical">
<com.example.titlelayout.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/but"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击进入" />
</LinearLayout>
MainActivity的代码:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView titleTextView;
private Button toBut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
toBut = (Button) findViewById(R.id.but);
titleTextView = (TextView) findViewById(R.id.title_text);
titleTextView.setText("First");
toBut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
它会跳转到第二个界面,第二个界面的xml文件second.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" >
<com.example.titlelayout.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="Second Activity"
/>
</LinearLayout>
下面是第二个页面的源文件,Second.java:
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
public class SecondActivity extends Activity{
private TextView tvView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.second);
tvView = (TextView) findViewById(R.id.title_text);
tvView.setText("Second");
}
}
程序是正常运行的,下面是程序运行结果:
直接上源代码吧!
首先是公用的title的布局文件title.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="wrap_content">
<Button
android:id="@+id/title_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:text="Back"
android:textColor="#fff" />
<TextView
android:id="@+id/title_text"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Title Text"
android:textColor="#ff22ff"
android:textSize="24sp" />
<Button
android:id="@+id/title_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:text="Edit"
android:textColor="#fff" />
</LinearLayout>
然后是写一个类用来完成需要完成的功能,比如返回到上一个界面,或者是显示其他的什么,都是可以的,类名TitleLayout.java:
code:
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class TitleLayout extends LinearLayout{
private Button backButton;
private Button editButton;
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);
backButton = (Button) findViewById(R.id.title_back);
editButton = (Button) findViewById(R.id.title_edit);
backButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
((Activity)getContext()).finish();
}
});
editButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getContext(), "you click the edit button", Toast.LENGTH_LONG).show();
}
});
}
}
随后是在需要的页面进行调用,其实也就是一个自定义的类。在MainActivity当中调用。就是在布局文件当中调用activity_main.xml:
code:
<LinearLayout 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"
android:orientation="vertical">
<com.example.titlelayout.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/but"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击进入" />
</LinearLayout>
MainActivity的代码:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView titleTextView;
private Button toBut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
toBut = (Button) findViewById(R.id.but);
titleTextView = (TextView) findViewById(R.id.title_text);
titleTextView.setText("First");
toBut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
它会跳转到第二个界面,第二个界面的xml文件second.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" >
<com.example.titlelayout.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="Second Activity"
/>
</LinearLayout>
下面是第二个页面的源文件,Second.java:
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
public class SecondActivity extends Activity{
private TextView tvView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.second);
tvView = (TextView) findViewById(R.id.title_text);
tvView.setText("Second");
}
}
程序是正常运行的,下面是程序运行结果:
相关文章推荐
- android 自定义控件字体,解决字体偏移,卡顿,代码重复等问题
- 利用委托与泛型解决功能代码重复问题
- android 自定义控件字体,解决字体偏移,卡顿,代码重复等问题
- 解决在VS(winform)程序中无法在调试时修改代码的问题(也就是“编辑并继续”功能无效)
- 生成动态库so的方法|解决NDK r5b引用静态库失效的问题|在NDK的Nactive代码中使用Android Log的方法|生成Native java头文件
- 解决android导入多个第三方jar包后项目代码混淆的问题 .
- 解决在MyEclipse安装Aptana之后无代码提示功能问题
- 解决Eclipse3.6中Android 代码自动补全卡死的问题
- ubuntu12.04 64bit编译android ics4.0代码出现的/usr/bin/ld.bfd.real: cannot find 问题解决
- Android:自定义Activity基类,复用布局/控件,减少设置控件的重复代码,onPostOnCreate
- Android 编程下引入第三方jar包进行代码混淆时的问题解决
- 关于android开发时,eclipse的函数自动提示功能很卡的问题解决方法
- Android中Toast重复显示问题解决
- myEclipse/eclipse代码提示、模板提示功能扩展与eclipse代码不能提示问题解决完整版
- 哥发明的 一个ssh整合时解决dao层代码冗余问题的"一点儿不漏"型抽取法!!!!
- 激活 iPhone通过 GPRS 连接服务器功能的代码--这个问题是很早以前的了 怀旧吧 早已解决了
- Android项目代码导入eclipse环境后的问题解决
- 解决Mono for android的xml编辑器无法代码完成的问题
- Andorid编程心得-利用代码解决Android WIFI休眠后连接断开的问题
- 相比xib 使用代码编排view 的一个明显的好处就是可以更好地重复使用已有代码,减少代码冗余。