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

时间:2015-01-07 09:57:06

标签: android android-layout

在我的XML布局中,我在SliderLayout中使用RelativeLayout,其中高度和宽度为match_parent。我有一个skip按钮来完成活动。我认为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;
    }
}
}

2 个答案:

答案 0 :(得分:1)

只需打开XML并在Button正文中添加SliderLayout即可。可以通过拖动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>

应该这样做。希望能帮助到你。 :)

答案 1 :(得分:0)

<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内移动按钮即可解决我的问题。