我想写一个类似QQ好友列表可以滑动的TAB界面,但写好后有一个问题,tab的选项卡被下面的内容覆盖了,如下图:图中那个btton是其中的一个Activity中的,按理说它不应该从最上面开始而已呀,请大神帮看看
下面是代码
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:id="@+id/touch_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- 这个LinerLayout 的目的是用来左右滑动切换选项卡的-->
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
> <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TabWidget> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
</FrameLayout>
</TabHost></LinearLayout>
tab_indector.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/tab_wight_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<!--
<ImageView
android:id="@+id/indector_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
-->
<TextView
android:id="@+id/indector_text"
android:layout_width="150dp"
android:layout_height="30dp"
android:gravity="center"
android:background="@android:color/transparent"
/>
</RelativeLayout>
MainActivity.java
package com.resume.activity;import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends TabActivity implements OnTouchListener,OnGestureListener{
//定义手势的最小距离和速度
private static final int FLING_MIN_DISTANCE = 20;
private static final int FLING_MIN_VELOCITY = 0;
//?
GestureDetector mGestureDetector;
//布局组件
private TabHost tabs;
private LinearLayout touchArea;
private ImageView tab_title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mGestureDetector=new GestureDetector(this);
//把触摸区域组件与触摸事件捆绑
touchArea=(LinearLayout)findViewById(R.id.touch_area);
touchArea.setOnTouchListener(this);
touchArea.setLongClickable(true);
tabs=getTabHost();
tabs.setup();
Intent intent=new Intent();
intent.setClass(this,IntroductionActivity.class);
tabs.addTab(createTab("introduction", intent));
intent=new Intent();
intent.setClass(this,ExperienceActivity.class);
tabs.addTab(createTab("experience", intent));
intent=new Intent();
intent.setClass(this,SkillAndJobActivity.class);
tabs.addTab(createTab("skill&job", intent));
}
private TabSpec createTab(String title,Intent intent){
TabSpec spec=tabs.newTabSpec("");
// tab_title.setText(title);
View v=LayoutInflater.from(this).inflate(R.layout.tab_indector, null);
TextView tv=(TextView)v.findViewById(R.id.indector_text);
tv.setText(title);
spec.setIndicator(v);
spec.setContent(intent);
return spec;
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("信息", "touch");
return mGestureDetector.onTouchEvent(event);
} @Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
} @Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
} @Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
} @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
} @Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
} @Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
int totle=tabs.getTabWidget().getChildCount();
int current=tabs.getCurrentTab();
if((e1.getX()-e2.getX())>FLING_MIN_DISTANCE&&Math.abs(velocityX)>FLING_MIN_VELOCITY){
Toast.makeText(getApplicationContext(), "left", Toast.LENGTH_LONG).show();
}else if((e2.getX()-e1.getX())>FLING_MIN_DISTANCE&&Math.abs(velocityX)>FLING_MIN_VELOCITY){
Toast.makeText(getApplicationContext(), "right", Toast.LENGTH_LONG).show();
}
return false;
}}Android布局覆盖
下面是代码
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:id="@+id/touch_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- 这个LinerLayout 的目的是用来左右滑动切换选项卡的-->
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
> <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TabWidget> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
</FrameLayout>
</TabHost></LinearLayout>
tab_indector.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/tab_wight_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<!--
<ImageView
android:id="@+id/indector_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
-->
<TextView
android:id="@+id/indector_text"
android:layout_width="150dp"
android:layout_height="30dp"
android:gravity="center"
android:background="@android:color/transparent"
/>
</RelativeLayout>
MainActivity.java
package com.resume.activity;import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends TabActivity implements OnTouchListener,OnGestureListener{
//定义手势的最小距离和速度
private static final int FLING_MIN_DISTANCE = 20;
private static final int FLING_MIN_VELOCITY = 0;
//?
GestureDetector mGestureDetector;
//布局组件
private TabHost tabs;
private LinearLayout touchArea;
private ImageView tab_title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mGestureDetector=new GestureDetector(this);
//把触摸区域组件与触摸事件捆绑
touchArea=(LinearLayout)findViewById(R.id.touch_area);
touchArea.setOnTouchListener(this);
touchArea.setLongClickable(true);
tabs=getTabHost();
tabs.setup();
Intent intent=new Intent();
intent.setClass(this,IntroductionActivity.class);
tabs.addTab(createTab("introduction", intent));
intent=new Intent();
intent.setClass(this,ExperienceActivity.class);
tabs.addTab(createTab("experience", intent));
intent=new Intent();
intent.setClass(this,SkillAndJobActivity.class);
tabs.addTab(createTab("skill&job", intent));
}
private TabSpec createTab(String title,Intent intent){
TabSpec spec=tabs.newTabSpec("");
// tab_title.setText(title);
View v=LayoutInflater.from(this).inflate(R.layout.tab_indector, null);
TextView tv=(TextView)v.findViewById(R.id.indector_text);
tv.setText(title);
spec.setIndicator(v);
spec.setContent(intent);
return spec;
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("信息", "touch");
return mGestureDetector.onTouchEvent(event);
} @Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
} @Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
} @Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
} @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
} @Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
} @Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
int totle=tabs.getTabWidget().getChildCount();
int current=tabs.getCurrentTab();
if((e1.getX()-e2.getX())>FLING_MIN_DISTANCE&&Math.abs(velocityX)>FLING_MIN_VELOCITY){
Toast.makeText(getApplicationContext(), "left", Toast.LENGTH_LONG).show();
}else if((e2.getX()-e1.getX())>FLING_MIN_DISTANCE&&Math.abs(velocityX)>FLING_MIN_VELOCITY){
Toast.makeText(getApplicationContext(), "right", Toast.LENGTH_LONG).show();
}
return false;
}}Android布局覆盖
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="introduction"/></RelativeLayout>
去掉的话,button会直接压到选项卡上面
touchArea 应该是这个linearlayout