我有一个 TabWidget
,里面有 5 个标签。 标签:“一”、“二”、“三”、“四”、“五”。 TabHost
位于 HorizontalScrollView
中。现在,我在 TabWidget
的每一侧各有一个箭头(< 和 >),以指示该方向上有更多选项卡(目前在 View 中不可见)。
现在我想做的是,如果第一个选项卡不可见(在当前 View 中),那么向左的箭头 (<) 应该是可见的,如果最后一个选项卡不可见( 在当前 View 中) 那么向右的箭头应该是可见的,如果两个选项卡都不可见(在当前 View 中)那么两个箭头都应该是可见的。我尝试这样做:
if (horizontablscrollview.getLeft == 0)
{
arrowRight.setVisiblity(View.VISIBLE)
arrowLeft.setVisiblity(View.INVISIBLE)
}
else if( horizontablscrollview.getRight == 0)
{
arrowLeft.setVisiblity(View.VISIBLE)
arrowRight.setVisiblity(View.INVISIBLE)
}
else if ( horizontablscrollview.getRight == 0 && horizontablscrollview.getLeft == 0)
{
arrowRight.setVisiblity(View.VISIBLE)
arrowLeft.setVisiblity(View.VISIBLE)
}
但这不起作用,我的两个标签在开始时都是不可见的:
我的 xml 看起来像这样..
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<RelativeLayout
android:id="@+id/RL_Header"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_marginBottom="3dip"
android:background="@drawable/gradient" >
<ImageButton
android:id="@+id/btn_Home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:background="@android:color/transparent"
android:contentDescription="@string/description_home"
android:onClick="onHomeButtonClick"
android:src="@drawable/title_home" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip"
android:background="@android:color/transparent"
android:contentDescription="@string/description_about"
android:onClick="onClickAbout"
android:src="@drawable/title_about" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/FL_Tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/RL_Header" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/FL_Tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/RL_Header" >
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<ImageView android:id="@+id/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/i_button"
android:layout_alignParentLeft="true"/>
<RelativeLayout
android:id="@+id/rl_tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView android:id="@+id/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/i_button"
android:layout_alignParentLeft="true"
android:visibility="invisible"/>
<ImageView android:id="@+id/arrow_Right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/i_button"
android:layout_alignParentRight="true"
android:visibility="invisible"/>
<HorizontalScrollView
android:id="@+id/my_scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none"
android:layout_toRightOf="@+id/arrow_left"
android:layout_toLeftOf="@+id/arrow_Right">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:tabStripEnabled="true"/>
</HorizontalScrollView>
</RelativeLayout>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="65dip" >
<RelativeLayout
android:id="@+id/myTabContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dip" >
<TableLayout
android:id="@+id/tabSubs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="20dip"
android:gravity="center_horizontal" >
<Button
android:id="@+id/btntestno1"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 1"
android:textColor="#000000" />
<Button
android:id="@+id/btntestno2"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 2"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:gravity="center_horizontal" >
<Button
android:id="@+id/btntestno3"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 3"
android:textColor="#000000" />
<Button
android:id="@+id/btntestno4"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 4"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:gravity="center_horizontal" >
<Button
android:id="@+id/btntestno5"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 5"
android:textColor="#000000" />
<Button
android:id="@+id/btntestno6"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 6"
android:textColor="#000000" />
</TableRow>
</TableLayout>
</RelativeLayout>
</FrameLayout>
</TabHost>
</RelativeLayout>
</RelativeLayout>
如有任何帮助,我们将不胜感激。
最佳答案
一个解决方案是让您拥有 HorizontalScrollView
类并显示基于滚动的 ImageViews
(尽管我没有测试下面的代码应该工作的那么多) .您的布局将如下所示:
//...
<RelativeLayout
android:id="@+id/rl_tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView android:id="@+id/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/i_button"
android:layout_alignParentLeft="true"
android:visibility="gone"/>
<ImageView android:id="@+id/arrow_Right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/i_button"
android:layout_alignParentRight="true"
/>
<com.your.package.here.SpecialScroll
android:id="@+id/my_scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:tabStripEnabled="true"/>
</com.your.package.here.SpecialScroll>
</RelativeLayout>
//...
SpecialScroll
类将如下所示:
public class SpecialScroll extends HorizontalScrollView {
public interface PositionListener {
public void onLeftArrowRequired(boolean required);
public void onRightArrowRequired(boolean required);
public View implementScrolledView();
}
private PositionListener listener;
public void setPositionListener(PositionListener listener) {
this.listener = listener;
}
public SpecialScroll(Context context) {
super(context);
}
public SpecialScroll(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SpecialScroll(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (l == 0) {
listener.onLeftArrowRequired(false);
} else {
listener.onLeftArrowRequired(true);
}
View v = listener.implementScrolledView();
Rect r = new Rect();
v.getDrawingRect(r);
if ((r.width() - l) == getWidth()) {
listener.onRightArrowRequired(false);
} else {
listener.onRightArrowRequired(true);
}
}
}
然后在您的 Activity 的 onCreate
方法中,您将执行以下操作:
SpecialScroll hsv = (SpecialScroll) findViewById(R.id.my_scrollView);
hsv.setPositionListener(this); // I made the activity implement the SpecialScroll.PositionListener interface
PositionListener
接口(interface)的 Activity 实现是:
@Override
public void onLeftArrowRequired(boolean required) {
if (required) {
((ImageView) findViewById(R.id.arrow_left))
.setVisibility(View.VISIBLE);
} else {
((ImageView) findViewById(R.id.arrow_left))
.setVisibility(View.GONE);
}
}
@Override
public void onRightArrowRequired(boolean required) {
if (required) {
((ImageView) findViewById(R.id.arrow_right))
.setVisibility(View.VISIBLE);
} else {
((ImageView) findViewById(R.id.arrow_right))
.setVisibility(View.GONE);
}
}
@Override
public View implementScrolledView() {
return findViewById(android.R.id.tabs);
}
关于android - 是否可以检查 TabWidget 中的特定 "TAB"在当前 View 中是否可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11051909/