我的应用程序中有一个水平滚动视图。在滚动视图的任一侧,我希望保留一个向左和向右箭头按钮(下面的ibLeft和ibRight),该按钮应始终可见,并且在任何时间都不应隐藏在水平滚动视图图像的后面。
我面临的问题是:
某种程度上,水平滚动视图使两个按钮重叠。
另外,为按钮设置onclick侦听器时,我看不到任何事情发生。我在某处读到,当我们使用水平滚动视图时,这是一个问题。
请帮助。
<?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" >
<RelativeLayout
android:id="@+id/RelBt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="NEWS" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="LIVE T.V." />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="YOUTH" />
</RelativeLayout>
<TextView
android:id="@+id/tvtmp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Video will come up here" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/RelBt"
android:layout_below="@+id/tvtmp" >
<ImageButton
android:id="@+id/ibLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/ibRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
<HorizontalScrollView
android:id="@+id/hsv"
android:layout_width="wrap_content"
android:fadingEdgeLength="100dp"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView..../>
<ImageView..../>
<ImageView..../>
<ImageView..../>
<ImageView..../>
</RelativeLayout>
</HorizontalScrollView>
</RelativeLayout>
</RelativeLayout>
Java文件是
public class Main extends Activity implements OnClickListener{
HorizontalScrollView hSV;
ImageButton ibLeft,ibRight;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ibLeft=(ImageButton) findViewById(R.id.ibLeft);
ibLeft.setOnClickListener(this);
ibRight=(ImageButton) findViewById(R.id.ibRight);
ibRight.setOnClickListener(this);
hSV=(HorizontalScrollView) findViewById(R.id.hsv);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.ibLeft){
Toast.makeText(Main.this, "Left", Toast.LENGTH_SHORT).show();
}
else if(v.getId()==R.id.ibLeft){
Toast.makeText(Main.this, "Right", Toast.LENGTH_SHORT).show();
}
}
}
最佳答案
将HorizontalScrollView
相对于ImageButtons
定位:
//...
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/RelBt"
android:layout_below="@+id/tvtmp" >
<ImageButton
android:id="@+id/ibLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/ibRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
<HorizontalScrollView
android:id="@+id/hsv"
android:layout_width="wrap_content"
android:fadingEdgeLength="100dp"
android:layout_toRightOf="@id/ibLeft"
android:layout_toLeftOf="@id/ibRight"
android:layout_height="wrap_content" >
//...
考虑到按钮未接收到单击事件,它们可能已被
HorizontalScrollView
覆盖,此外,您还为onClick()
方法中的左右两个按钮设置了相同的(左侧按钮的)id。