按钮重叠

时间:2017-02-23 02:49:35

标签: android button chronometer

我使用带有四个按钮的天文台制作秒表但是当我使用可见性模式使停止和暂停按钮出现时它们重叠....请解释为什么......下面是代码.....

假设布局文件包含相对布局的按钮......

public class StopWatchFragment extends Fragment {

Chronometer chronometer;
Button startStopWatch;
Button stopStopWatch;
Button resetStopWatch;
Button pauseStopWatch;
Button resumeStopWatch;
private long lastPause;
//RelativeLayout relativeLayout;
private int check = 0;

public StopWatchFragment() {
    // Required empty public constructor
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.stopwatch_layout,container,false);
chronometer = (Chronometer) rootView.findViewById(R.id.stopwatch);
startStopWatch = (Button) rootView.findViewById(R.id.startStopWatch);
stopStopWatch = (Button) rootView.findViewById(R.id.stopStopWatch);
resetStopWatch = (Button) rootView.findViewById(R.id.resetStopWatch);
pauseStopWatch = (Button) rootView.findViewById(R.id.pauseStopWatch);
resumeStopWatch = (Button) rootView.findViewById(R.id.resumeStopWatch);
relativeLayout = (RelativeLayout) rootView.findViewById(R.id.parentRelativeLayout);
pauseStopWatch.setVisibility(View.GONE);

//final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(relativeLayout.getLayoutParams());

startStopWatch.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        chronometer.setBase(SystemClock.elapsedRealtime());
        chronometer.start();
        startStopWatch.setVisibility(View.GONE);
        pauseStopWatch.setVisibility(View.VISIBLE);
        if(check == 1){
            resumeStopWatch.setClickable(true);
        }
        else{
            resumeStopWatch.setClickable(false);
        }
        //params.setMargins(16,16,16,16);
        //pauseStopWatch.setLayoutParams(params);
    }
});

pauseStopWatch.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        check = 1;
        lastPause = SystemClock.elapsedRealtime();
        chronometer.stop();
    }
});

resumeStopWatch.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        chronometer.setBase(chronometer.getBase() + SystemClock.elapsedRealtime() - lastPause);
        chronometer.start();
        resumeStopWatch.setClickable(false);
    }
});

stopStopWatch.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        chronometer.stop();
        startStopWatch.setVisibility(View.VISIBLE);
        pauseStopWatch.setVisibility(View.GONE);
        resumeStopWatch.setClickable(false);
    }
});

resetStopWatch.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        chronometer.setBase(SystemClock.elapsedRealtime());
        chronometer.stop();
        resumeStopWatch.setClickable(false);
        startStopWatch.setVisibility(View.VISIBLE);
        pauseStopWatch.setVisibility(View.GONE);
    }
});

return rootView;
}
}

这是布局文件请参考....

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/parentRelativeLayout"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_horizontal_margin">

    <Chronometer
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:textAlignment="center"
        android:id="@+id/stopwatch"
        android:layout_margin="16dp"/>

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/stopwatch">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/startStopWatch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Start"
                android:background="@drawable/buttonshape"
                android:textSize="24sp" />

            <Button
                android:id="@+id/pauseStopWatch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Pause"
                android:background="@drawable/buttonshape"
                android:textSize="24sp" />

            <Button
                android:id="@+id/stopStopWatch"
                android:layout_marginTop="16dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/startStopWatch"
                android:background="@drawable/buttonshape"
                android:text="Stop"
                android:textSize="24sp"/>

            <Button
                android:id="@+id/resetStopWatch"
                android:layout_alignParentRight="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/buttonshape"
                android:text="Reset"
                android:textSize="24sp" />

            <Button
                android:id="@+id/resumeStopWatch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/buttonshape"
                android:text="Resume"
                android:textSize="24sp"
                android:layout_marginTop="16dp"
                android:layout_alignParentRight="true"
                android:layout_below="@id/resetStopWatch"/>


        </RelativeLayout>

    </ScrollView>


</RelativeLayout>

This is the way buttons work when click on start button

2 个答案:

答案 0 :(得分:0)

您正在隐藏按钮,但您仍在使用OnClickListener进行侦听。尝试添加:

pauseStopWatch.setOnClickListener(null);

然后,当您按下按钮时:

pauseStopWatch.setOnClickListener(whatever);

答案 1 :(得分:0)

试试这个布局

 $.getJSON("http://api.bandsintown.com/events/search.json?&api_version=2.0&app_id=FYP&location=Dublin,Ireland", function(result) 

在上文中,<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:id="@+id/parentRelativeLayout" android:layout_height="match_parent" android:padding="@dimen/activity_horizontal_margin"> <Chronometer android:layout_centerInParent="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" android:textAlignment="center" android:id="@+id/stopwatch" android:layout_margin="16dp"/> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/stopwatch"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:gravity="center" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:id="@+id/startStopWatch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start" android:background="@drawable/buttonshape" android:textSize="24sp" /> <Button android:visibility="gone" android:id="@+id/pauseStopWatch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pause" android:background="@drawable/buttonshape" android:textSize="24sp" /> <Button android:id="@+id/stopStopWatch" android:layout_marginTop="16dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/startStopWatch" android:background="@drawable/buttonshape" android:text="Stop" android:textSize="24sp"/> </LinearLayout> <LinearLayout android:gravity="center" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:id="@+id/resetStopWatch" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/buttonshape" android:text="Reset" android:textSize="24sp" /> <Button android:id="@+id/resumeStopWatch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/buttonshape" android:text="Resume" android:textSize="24sp" android:layout_marginTop="16dp" android:layout_alignParentRight="true" android:layout_below="@id/resetStopWatch"/> </LinearLayout> </LinearLayout> 按钮将设置为pause。您稍后在用户点击Gone按钮

时可以看到它