我有一个简单的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>
相关片段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; }}
谢谢
解决方法:
只为角点击使用这种逻辑
<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>
总结 以上是内存溢出为你收集整理的java – Android按钮只能在角落上点击全部内容,希望文章能够帮你解决java – Android按钮只能在角落上点击所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)