Android按钮只能在角落上点击

时间:2018-05-16 01:18:02

标签: java android android-layout

我有一个带有几个按钮的简单LinearLayout,其中状态颜色/文本根据底层服务的状态而变化,这样可以正常工作。

然而按钮只能在右上角点击???

按钮allSystemServicesToggleButton,我已经在这篇文章中包含了实现,只在右侧/右侧点击了???

这是我的片段xml布局& “Show Layout bounds”设置为true的实际屏幕截图:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView

        android:layout_height="wrap_content"

        android:text="All System Services"
        android:textColor="#000000"
        android:textSize="20sp"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:singleLine="true"
        android:onClick="onClick"/>

    <Button
        android:id="@+id/allSystemServicesToggleButton"
        android:layout_height="wrap_content"
        android:text="@string/stopped"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:backgroundTint="@color/stoppedServiceColor"
        android:enabled="true"/>



</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView

        android:layout_height="wrap_content"
        android:text="@string/shutdown_all_services"
        android:textColor="#000000"
        android:textSize="20sp"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:singleLine="true"
        android:onClick="onClick"/>

    <Button
        android:id="@+id/shutdownAllServicesToggleButton"
        android:layout_height="wrap_content"
        android:text="@string/shutdown"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:enabled="true"/>



</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:text="Networks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="32sp"
        android:textColor="#000000"/>

    <View
        android:id="@+id/viewServicesDivider1"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#808080"
        android:layout_gravity="center"
        />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:text="Bluetooth Service"
        android:textColor="#000000"
        android:textSize="20sp"
        android:singleLine="true"/>

    <Button
        android:id="@+id/btServicesToggleButton"
        android:layout_height="wrap_content"
        android:text="@string/stopped"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:backgroundTint="@color/stoppedServiceColor"/>

</LinearLayout>


<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
<!--TODO: get requirements for showing paired devices & pairing devices-->
<TextView
    android:id="@+id/textPairedText"
      android:layout_height="wrap_content"
      android:layout_weight="5"
      android:text="Paired Bluetooth Devices"
      android:textColor="#000000"
      android:singleLine="true"
      android:textSize="20sp"
    android:layout_width="0dp"
      />

  <TextView
      android:id="@+id/textViewNumberOfConnectedDevices"
      android:layout_height="wrap_content"
      android:text="0"
      android:layout_width="0dp"
      android:layout_weight="1" />

  <Button
      android:id="@+id/btDevicesToggleButton"
      android:layout_height="wrap_content"
      android:layout_width="0dp"
      android:layout_weight="2"
      android:text="Pair"

      />

</LinearLayout>



<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:text="MQTT Service"
        android:textColor="#000000"
        android:textSize="20sp"
        android:singleLine="true"/>

    <Button
        android:id="@+id/MQTTserviceToggleButton"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:backgroundTint="@color/stoppedServiceColor"
        android:text="@string/stopped" />

</LinearLayout>



<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:text="Location Services"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="32sp"
        android:textColor="#000000"/>

    <View
        android:id="@+id/viewServicesDivider3"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#808080"
        android:layout_gravity="center"
        />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:text="GPS"
        android:textColor="#000000"
        android:textSize="20sp"
        android:singleLine="true"/>

    <Button
        android:id="@+id/gpsServiceToggleButton"
        android:layout_height="wrap_content"
        android:text="@string/stopped"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:backgroundTint="@color/stoppedServiceColor"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:text="Command Services"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="32sp"
        android:textColor="#000000"/>

    <View
        android:id="@+id/viewServicesDivider4"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#808080"
        android:layout_gravity="center"
        />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:text="Voice Recognition"
        android:textColor="#000000"
        android:textSize="20sp"
        android:singleLine="true"/>

    <Button
        android:id="@+id/voiceRecognitionToggleButton"
        android:layout_height="wrap_content"
        android:text="@string/stopped"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:backgroundTint="@color/stoppedServiceColor"
        />

</LinearLayout>

enter image description here

相关片段java:

package x.core.fragments;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.Fragment;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import x.core.Application.NgfrApp;
import x.core.R;
import x.core.helpers.Util;
import x.core.services.BluetoothService;
import x.core.services.LocationService;
import x.core.services.MqttBrokerService;
import x.core.services.ServicesStateBroadcastReceiver;
import x.core.services.SpeechRecognitionService;
import x.core.services.UIService;


public class ServicesFragment extends Fragment implements View.OnClickListener {

    private static final String TAG = "ServicesFragment";

    public static ServicesFragment newInstance() {
        return new ServicesFragment();
    }

    private static Button btServicesToggleButton;
    private static Button mqttServicesToggleButton;
    private static Button gpsServiceToggleButton;
    private static Button voiceServiceToggleButton;
    private static Button allServiceToggleButton;


    private static String stopped = null;
    private static String running = null;
    private static int runningColorId, stoppedColorId = -1;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_services, container, false);
        btServicesToggleButton = rootView.findViewById(R.id.btServicesToggleButton);
        mqttServicesToggleButton = rootView.findViewById(R.id.MQTTserviceToggleButton);
        gpsServiceToggleButton = rootView.findViewById(R.id.gpsServiceToggleButton);
        voiceServiceToggleButton = rootView.findViewById(R.id.voiceRecognitionToggleButton);
        allServiceToggleButton = rootView.findViewById(R.id.allSystemServicesToggleButton);
        stopped = getResources().getString(R.string.stopped);
        running = getResources().getString(R.string.running);
        runningColorId = getResources().getColor(R.color.runningServiceColor);
        stoppedColorId = getResources().getColor(R.color.stoppedServiceColor);
        allServiceToggleButton.setEnabled(true);
        allServiceToggleButton.setClickable(true);
        allServiceToggleButton.setOnClickListener(this);

        return rootView;


    }


    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.allSystemServicesToggleButton:
                if (ServicesStateBroadcastReceiver.BT_SERVICE_STATE_VALUE==false ||  ServicesStateBroadcastReceiver.MQTT_STATE_VALUE==false || ServicesStateBroadcastReceiver.NGFR_GPS_SERVICE_STATE_VALUE==false || ServicesStateBroadcastReceiver.VOICE_SERVICE_STATE_VALUE==false)
                {

         Toast.makeText(NgfrApp.getContext(),NgfrApp.getContext().getResources().getString(R.string.restarting_services),Toast.LENGTH_SHORT).show();    
                 //restartingServices(); 
        }
        else
                {
                    Toast.makeText(NgfrApp.getContext(),NgfrApp.getContext().getResources().getString(R.string.all_already_running),Toast.LENGTH_SHORT).show();
                }
                break;
                default: 
                    break; 
        }
    }

}

MainActivity.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <android.support.design.widget.TabLayout
        android:id="@+id/activity_main_tabLyout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed" />

    <android.support.v4.view.ViewPager
        android:id="@+id/activity_main_viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />



</LinearLayout>

MainActivity.java,我只包含相关代码:

public class MainActivity extends  AppCompatActivity {
    private static String TAG = "Main";

    private static final int CHECK_BT_CODE = 1;
    private static final int CHECK_TTS_CODE = 2;
    //global boolean flags that will communicate the state of the system at all times
    //bluetooth related flags
    public boolean isBleSupported = false;
    public boolean isBluetoothEnabled = false;
    public boolean accessBluetoothManager= false;
    public boolean nearbyDevices = false;
    //configuration data related
    public boolean isConfigurationLoadedCorrectly = false;
    //text to speech related
    public boolean isTextToSpeechSupported = false;


    private Context context = null;
    private ServicesStateBroadcastReceiver servicesStateBroadcastReciever = null;
    private ViewPager mainViewPager;
    private TabLayout tabLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "Activity started!!");
        context = this;
        setContentView(R.layout.activity_main);

        MainActivityViewPager adapter = new MainActivityViewPager(getSupportFragmentManager());

        mainViewPager = (ViewPager) findViewById(R.id.activity_main_viewPager);
        mainViewPager.setAdapter(adapter);

        tabLayout = (TabLayout)  findViewById(R.id.activity_main_tabLyout);
        tabLayout.setupWithViewPager(mainViewPager );
}

}

我的片段的适配器,FragmentStatePagerAdapter:

package x.core.views;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import x.BiometricsFragment;
import x.ServicesFragment;

public class MainActivityViewPager extends FragmentStatePagerAdapter {
    public MainActivityViewPager(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment returnFragment;

        switch(position) {
            case 0:
                returnFragment = ServicesFragment.newInstance();
                break;
            case 1:
                returnFragment = BiometricsFragment.newInstance();
                break;
            default:
                return null;
        }

        return returnFragment;
    }

    @Override
    public int getCount() {
        return 2;
    }

    public CharSequence getPageTitle(int position) {
        CharSequence title;
        switch (position) {
            case 0:
                title = "Services";
                break;
            case 1:
                title = "Biometrics";
                break;
            default:
                return null;
        }

        return title;


    }
}

由于

1 个答案:

答案 0 :(得分:2)

仅用于角点击使用这种逻辑

<FrameLayout
                android:layout_width="50dp"
                android:layout_height="50dp">


                <TextView
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:background="@color/colorAccent" />

                <TextView
                    android:id="@+id/tvtttt"
                    android:layout_width="10dp"
                    android:layout_height="10dp"
                    android:layout_gravity="right"
                    android:background="#F00" />


            </FrameLayout>