我找不到解决方案,从中找到随机浮点数[0,a],其中a是用户定义的浮点数.
我尝试了以下内容,但它似乎无法正常工作.
float x=(float)rand()/((float)RAND_MAX/a)
Run Code Online (Sandbox Code Playgroud) 有没有办法在xml文件中获取android中的导航栏大小?
android:paddingTop="?android:attr/actionBarSize"
Run Code Online (Sandbox Code Playgroud)
哪里actionBarSize是导航栏大小?
我希望我的片段看起来与我应用的其他应用程序和调色板一致,所以我想改变标题的颜色,还要改变正/负按钮的颜色:

我尝试这样做,但不幸的是它不起作用:
public void onStart() {
super.onStart();
Dialog d = getDialog();
int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = d.findViewById(dividerId);
if(currentapiVersion< Build.VERSION_CODES.LOLLIPOP) {
divider.setBackgroundColor(getResources().getColor(R.color.accent));
LinearLayout ll = (LinearLayout) d.findViewById(R.id.dialog_background);
ll.setBackgroundResource(R.drawable.backrepeat_reversed);
}
if(currentapiVersion == Build.VERSION_CODES.LOLLIPOP) {
int buttonsId = d.getContext().getResources().getIdentifier("android:id/negativeButton", null, null);
Button b = (Button) d.findViewById(buttonsId);
b.setTextColor(getResources().getColor(R.color.accent));
}
int textViewId = d.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = (TextView) d.findViewById(textViewId);
tv.setTextColor(getResources().getColor(R.color.accent));
}
Run Code Online (Sandbox Code Playgroud)
如何更改这些按钮的颜色?也许可以通过styles.xml文件在整个应用程序中完成它?
android android-dialog android-dialogfragment android-5.0-lollipop
我想将自定义字体应用于我的应用程序的标题,该标题显示在ActionBar上.以前我没有使用任何支持库和这个解决方案:
int titleId = getResources().getIdentifier("action_bar_title", "id",
"android");
TextView yourTextView = (TextView) findViewById(titleId);
yourTextView.setTypeface(face);
Run Code Online (Sandbox Code Playgroud)
对我来说很好.但是现在我使用材质设计SupportActionBar,这会抛出NullPointerException.那么如何在使用AppCompat时更改ActionBar字体?
android android-layout android-fonts android-actionbar android-actionbar-compat
我想将CheckBox"符号" 对齐到其描述的顶部.
当前的xml:
<CheckBox android:id="@+id/register_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/register_hint"/>
Run Code Online (Sandbox Code Playgroud)
我试图通过操纵layout_gravity属性来实现这一点,但它不起作用.
我在Android Studio中生成签名APK时遇到问题.在解决了所有警告后,我被困在这个:
错误:任务':app:proguardRelease'的执行失败.java.io.FileNotFoundException:/Users/franek/Documents/Android_Studio_Melange/app/proguard-rules.txt(没有这样的文件或目录)
我不想将minifyEnabled改为false,因为我想让Proguard保持工作.我该如何解决这个错误?
build.gradle的一个片段:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,我可以在其中查找和配置BLE设备.我使用标准的Android BLE API,但最近我遇到了一些奇怪的问题.
当我打开我的应用程序时,BLE扫描工作正常.我正在扫描使用:
mBluetoothAdapter.startLeScan(mLeScanCallback); // for Kitkat and below
Run Code Online (Sandbox Code Playgroud)
和
mBluetoothAdapter.getBluetoothLeScanner().startScan(mScanCallback); // for Lollipop and above
Run Code Online (Sandbox Code Playgroud)
在Logcat中我收到以下消息(我猜这对于这个问题很重要):
D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,我还可以从我的BLE设备中读取某些特征(例如电池状态).我使用以下命令连接到设备以在单独的片段中读取此特征:
mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(mMacAddress);
mBluetoothGatt = mBluetoothDevice.connectGatt(mContext, false, mGattCallback);
Run Code Online (Sandbox Code Playgroud)
正确读取特征.在onCharacteristicRead回调中我也断开并关闭Gatt:
mBluetoothGatt.disconnect();
mBluetoothGatt.close();
Run Code Online (Sandbox Code Playgroud)
每次打开一个片段来读取一个特征(无论它是否是同一个设备),clientIf值都会增加.我可以在LogCat中看到:
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=9
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=10
Run Code Online (Sandbox Code Playgroud)
一切正常,直到clientIf值等于10. BLE扫描停止查找任何设备,我无法连接到我的任何设备读取任何特征等.而LogCat无限显示这些消息:
D/BluetoothGatt: unregisterApp() - mClientIf=0 …Run Code Online (Sandbox Code Playgroud) 我正在使用这段代码来检测Internet连接状态的变化.它适用于Android <5.0,但在API 21上:
intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY)
Run Code Online (Sandbox Code Playgroud)
总是假的.如何使此代码在Android 5.0上运行?
我的BroadcastReceiver:
public class NetworkStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
if(intent.getExtras()!=null) {
final ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo != null && networkInfo.isConnectedOrConnecting()) {
Log.d("receiver test", "detected on");
}
}
Log.d("receiver test", Boolean.toString(intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY)));
if(intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
Log.d("receiver test", "detected off");
}
}
}
Run Code Online (Sandbox Code Playgroud) android broadcastreceiver android-internet android-5.0-lollipop
我有一个问题,因为当我在片段中调用它时,getTag()方法返回null,这是我创建的ViewPager选项卡布局的一部分.
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
public class HistoryFragment extends Fragment {
ListView listView;
HistoryAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View history = inflater.inflate(R.layout.fragment_history, container, false);
ArrayList<ToSave> arrayOfData = new ArrayList<ToSave>();
adapter = new HistoryAdapter(getActivity().getBaseContext(), arrayOfData);
listView = (ListView) history.findViewById (R.id.listView1);
listView.setAdapter(adapter);
String myTag = getTag();
((MainActivity)getActivity()).setHistoryFragment(myTag);
Toast.makeText(getActivity(), "HistoryFragment.onCreateView(): " + myTag, Toast.LENGTH_LONG).show();
return history;
}
}
Run Code Online (Sandbox Code Playgroud)
我想用它来进行Fragments之间的通信(通过单击另一个片段中的按钮来添加ListView项目),但我无法使其工作.
android android-layout android-listview android-fragments android-viewpager
我在一个没有Gradle的IntelliJ中开发的旧项目有问题.我想将它迁移到Android Studio和Gradle,但我遇到了很多问题.由于该项目已经过时,因此使用了旧的Google Play服务版本.在Intellij中,我刚刚将旧gps的libproject添加到依赖项(google_play_services_5089000_r19),一切正常.在Android Studio中,我设法通过将其添加为库模块并添加compile project(':segmentedradios')为gradle依赖项来添加其他库,但我无法使gps库工作.我曾尝试将其添加为模块,但Android Studio在指向libroject库目录后表示"未选择模块" .我也尝试将其添加为gradle依赖项,但我不断遇到这样的错误:
error: package com.google.android.gcm does not exist
error: package com.google.android.maps does not exist
error: cannot find symbol variable GCMRegistrar
Run Code Online (Sandbox Code Playgroud)
尽管我尝试了10种不同的解决方案,但该项目仍然无效.怎么解决?
摇篮:
apply plugin: 'com.android.application'
android {
compileSdkVersion "Google Inc.:Google APIs:18"
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "my_package.app_name"
minSdkVersion 14
targetSdkVersion 18
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:support-v4:18.0.0'
compile files('libs/libGoogleAnalyticsV2.jar')
compile project(':segmentedradios')
compile 'com.google.android.gms:play-services:5.0.89'
}
Run Code Online (Sandbox Code Playgroud) android android-build google-play-services android-gradle-plugin