Android 按钮点击不适用于 RelativeLayout 中的 SliderLayout

Android Button click does not work with SliderLayout in RelativeLayout

在我的 XML 布局中,我在 RelativeLayout 中使用 SliderLayout,其中高度和宽度为 match_parent。我有一个 skip 按钮来完成 activity。我认为 SliderLayout 覆盖了 skip 按钮,所以我不能使用按钮的 click 事件。这没用。我怎样才能避免这个问题?

activity_tutorial.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"
android:background="@color/White" >

<Button
    android:id="@+id/skipButton"
    style="@style/button_type"
    android:layout_width="@dimen/dp_150"
    android:layout_height="@dimen/dp_40"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="@dimen/dp_20"
    android:text="@string/skip" />

<com.daimajia.slider.library.SliderLayout
    android:id="@+id/imageSlider"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true" />

<com.daimajia.slider.library.Indicators.PagerIndicator
    android:id="@+id/custom_indicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

TutorialActivity.java

public class TutorialActivity extends Activity implements
    OnSliderClickListener, OnClickListener {
private static final String FONTH_PATH_BUTTON = "fonts/Brandon_bld.otf";

private Typeface buttonFont;
private Button skipButton;
private SliderLayout mDemoSlider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tutorial);

    buttonFont = Typeface.createFromAsset(getAssets(), FONTH_PATH_BUTTON);

    skipButton = (Button) findViewById(R.id.skipButton);
    skipButton.setTypeface(buttonFont);
    skipButton.setOnClickListener(this);

    mDemoSlider = (SliderLayout) findViewById(R.id.imageSlider);

    HashMap<String, Integer> file_maps = new HashMap<String, Integer>();
    file_maps.put("Challenge", R.drawable.tutorial_challenge);
    file_maps.put("Select", R.drawable.tutorial_select);
    file_maps.put("Image", R.drawable.tutorial_image);
    file_maps.put("Friend", R.drawable.tutorial_friend);

    for (String name : file_maps.keySet()) {
        TextSliderView textSliderView = new TextSliderView(this);
        // initialize a SliderLayout
        textSliderView.description(name).image(file_maps.get(name))
                .setScaleType(BaseSliderView.ScaleType.Fit)
                .setOnSliderClickListener(this);

        // add your extra information
        textSliderView.getBundle().putString("extra", name);

        mDemoSlider.addSlider(textSliderView);
    }

    mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
    mDemoSlider
            .setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
    mDemoSlider.setCustomAnimation(new DescriptionAnimation());
    mDemoSlider.setDuration(4000);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.tutorial, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onSliderClick(BaseSliderView slider) {
    // TODO Auto-generated method stub

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.skipButton:
        finish();
        break;

    default:
        break;
    }
}
}
<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"
android:background="@color/White" >

<com.daimajia.slider.library.SliderLayout
    android:id="@+id/imageSlider"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true" >

    <Button
        android:id="@+id/skipButton"
        style="@style/button_type"
        android:layout_width="@dimen/dp_150"
        android:layout_height="@dimen/dp_40"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/dp_10"
        android:text="@string/skip" />
</com.daimajia.slider.library.SliderLayout>

<com.daimajia.slider.library.Indicators.PagerIndicator
    android:id="@+id/custom_indicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

只需在 SliderLayout 中移动按钮即可解决我的问题。

只需打开 XML 并在 SliderLayout 正文中添加 Button。可以通过拖动 SliderLayout.

上方的按钮进行设置

您的代码:

<Button
    android:id="@+id/skipButton"
    style="@style/button_type"
    android:layout_width="@dimen/dp_150"
    android:layout_height="@dimen/dp_40"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="@dimen/dp_20"
    android:text="@string/skip" />

<com.daimajia.slider.library.SliderLayout
    android:id="@+id/imageSlider"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true" />

改为:

<com.daimajia.slider.library.SliderLayout
    android:id="@+id/imageSlider"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true" >

    <Button
        android:id="@+id/skipButton"
        style="@style/button_type"
        android:layout_width="@dimen/dp_150"
        android:layout_height="@dimen/dp_40"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/dp_10"
        android:text="@string/skip" />
</com.daimajia.slider.library.SliderLayout>

应该可以了。希望能帮助到你。 :)