小编Zie*_*iem的帖子

带有wrap_contents的斜体TextView似乎在右边缘剪切文本

<TextView android:id="@+id/prodLbl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textColor="#FFFFFF"
    android:textSize="30dip"
    android:textStyle="italic"
    android:text="Magnifico"
    />
Run Code Online (Sandbox Code Playgroud)

似乎从最右边的角色剪辑几个像素,至少在480x800模拟器或Nexus One上.

对我来说它看起来像一个bug,但我只是一个Android初学者.我试图向左右添加边距,但它仍然保持裁剪.最后,我的黑客是在文本的两边添加一个空格.还有其他方法吗?

android textview italic

51
推荐指数
6
解决办法
1万
查看次数

有没有办法从堆栈顶部获取片段?

我的MainActivity中有一个ListFragment.这是我设置片段对象的方法.

FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment newFragment = new MyFragment();
fragmentTransaction.replace(R.id.framecontainer, newFragment, "tag");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Run Code Online (Sandbox Code Playgroud)

问题是当用户按下后退按钮时,我必须至少通过调用来更改操作栏和菜单

getActionBar().setTitle(title);
getActionBar().setDisplayHomeAsUpEnabled(isEnabled);
invalidateOptionsMenu();
Run Code Online (Sandbox Code Playgroud)

我必须知道当前显示的是哪种片段,以便我知道如何设置操作栏.我将设置选项存储在fragment中作为参数.

String title = fragment.getArguments().getString("KEY_TITLE");
boolean isEnabled = fragment.getArguments().getBoolean("KEY_ISENABLED");
Run Code Online (Sandbox Code Playgroud)

我确实搜索了相关的问题,我意识到我可以通过调用来获取片段

MyFragment fragment = (MyFragment) getSupportFragmentManager()
        .findFragmentByTag("tag");
Run Code Online (Sandbox Code Playgroud)

但是,我必须将所有标记存储在自定义堆栈中,并在每次用户按下后退按钮时调用pop()onBackPressed().

所以,我的问题是,有没有办法让我直接从堆栈中获取当前可见的片段?

注意:请记住片段类型不同,而不仅仅是MyFragment.

android android-fragments

41
推荐指数
2
解决办法
5万
查看次数

如何忽略错误并继续无限流?

我想知道如何忽略异常并继续无限流(在我的情况下,位置流)?

我正在获取当前用户位置(使用Android-ReactiveLocation),然后将它们发送到我的API(使用Retrofit).

在我的情况下,当在网络调用(例如超时)期间发生异常时,调用onError方法并且流自行停止.怎么避免呢?

活动:

private RestService mRestService;
private Subscription mSubscription;
private LocationRequest mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(100);
...
private void start() {
    mRestService = ...;
    ReactiveLocationProvider reactiveLocationProvider = new ReactiveLocationProvider(this);
    mSubscription = reactiveLocationProvider.getUpdatedLocation(mLocationRequest)
            .buffer(50)
            .flatMap(locations -> mRestService.postLocations(locations)) // can throw exception
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe();
}
Run Code Online (Sandbox Code Playgroud)

RestService:

public interface RestService {
    @POST("/.../")
    Observable<Response> postLocations(@Body List<Location> locations);
}
Run Code Online (Sandbox Code Playgroud)

android rx-java rx-android

39
推荐指数
4
解决办法
3万
查看次数

如何使任何视图绘制到画布?

我有一个简短的问题:

假设我有一个(可变的)位图,我需要修改(添加图像,文本等...).

我没有考虑使用许多特殊的类来绘制画布(画布,画布,矩阵等),而是考虑为什么不使用Android的内置类来完成这项任务,只有当我需要真正定制的操作时,我仍然可以使用画布?

因此,例如,为了在位图上显示任何类型的视图(当然没有父视图),我可以调用下一个函数:

public void drawViewToBitmap(Bitmap b, View v, Rect rect) {
    Canvas c = new Canvas(b);
    // <= use rect to let the view to draw only into this boundary inside the bitmap
    view.draw(c);
}
Run Code Online (Sandbox Code Playgroud)

这样的事情可能吗?也许这就是它在幕后工作的方式?

我应该在绘图和画布创作之间的部分写什么?


编辑:我已经尝试了下一个代码,但它没有工作:

public void drawFromViewToCanvas(final View view, final Rect rect, final Canvas canvas) {
    final int widthSpec = View.MeasureSpec.makeMeasureSpec(rect.width(), View.MeasureSpec.EXACTLY);
    final int heightSpec = View.MeasureSpec.makeMeasureSpec(rect.height(), View.MeasureSpec.EXACTLY);
    view.measure(widthSpec, heightSpec);
    // Lay the view out with the known dimensions
    view.layout(0, 0, rect.width(), rect.height());
    // …
Run Code Online (Sandbox Code Playgroud)

android bitmap mutable draw android-canvas

37
推荐指数
1
解决办法
4万
查看次数

如何使用Libgdx/Java绘制文本?

我在使用Libgdx绘制如何绘制简单的2D文本时遇到了很多麻烦.这是我到目前为止编写的代码:

SpriteBatch spriteBatch;
BitmapFont font;
CharSequence str = "Hello World!";
spriteBatch = new SpriteBatch();
font = new BitmapFont();

spriteBatch.begin();
font.draw(spriteBatch, str, 10, 10);
spriteBatch.end();
Run Code Online (Sandbox Code Playgroud)

代码确实绘制了Hello World字符串,但是它会弄乱我所有的其他绘图.他们在那里,只是残忍地残缺,并且所有这一切都在移动.我试着Gdx.gl11.glPushMatrix()Gdx.gl11.glPopMatrix()周围只是陈述的每一个子集.

我已经将残缺不全的图纸缩小到了font.draw()电话,如果它被取出,一切正常(但当然没有文字).

java graphics libgdx

30
推荐指数
4
解决办法
5万
查看次数

Android 6:不能再共享文件了?

我正在共享一个图像,这段代码适用于Android 6之前的设备:

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri uri = Uri.fromFile(new File(mFilename));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
mContext.startActivity(Intent.createChooser(shareIntent, mChooserTitle));
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用Android 6共享时,我收到toast错误" 无法附加空文件 ".

我确认该文件存在且不是零长度.

有人有解决方案吗?

android share android-intent android-6.0-marshmallow

30
推荐指数
2
解决办法
1万
查看次数

intent.putExtra()在待处理的意图中不起作用

我通过alarmreceiver从服务类传递一个待处理的意图.但是,在pendingIntent触发后,broadcastreceiver类没有接收到intent.putExtra()信息.这是我的用于触发pendingIntent的代码

Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
Run Code Online (Sandbox Code Playgroud)

警报接收器类如下

public String msg, phonen;

@Override
public void  onReceive(Context context, Intent intent){
    Bundle extras = intent.getExtras();
    msg = extras.getString("msg");
    phonen = extras.getString("phone");

    Log.d("onReceive", "About to execute MyTask");
    Toast.makeText(context,msg, Toast.LENGTH_LONG).show();
}
Run Code Online (Sandbox Code Playgroud)

未显示正在从待处理意图接收的Toast中的msg信息.而是显示空白吐司.

android android-intent

25
推荐指数
2
解决办法
1万
查看次数

如何设置点击监听器以进行通知?

通过AlarmManager启动服务时,我使用以下代码启动通知:

nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "App";
CharSequence message = "Getting Latest Info...";
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
Notification notif = new Notification(R.drawable.icon,
    "Getting Latest Info...", System.currentTimeMillis());
notif.setLatestEventInfo(this, from, message, contentIntent);
nm.notify(1, notif);
Run Code Online (Sandbox Code Playgroud)

如何设置此项目的意图,以便当用户点击它时,它会启动某个活动?

android notificationmanager

23
推荐指数
3
解决办法
8万
查看次数

改造"IllegalStateException:已经执行"

我有一个Retrofit网络调用,id喜欢每5秒运行一次.我目前的代码:

Handler h = new Handler();
int delay = 5000; //milliseconds

h.postDelayed(new Runnable() {
    public void run() {
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(Response<ApiResponse> response) {
                Log.d("api", "response: " + response.body().getPosition().getLatitude().toString());
            }

            @Override
            public void onFailure(Throwable t) {

            }
        });
        h.postDelayed(this, delay);
    }
}, delay);
Run Code Online (Sandbox Code Playgroud)

这运行一次,但随后抛出以下内容:

java.lang.IllegalStateException:已经执行.at retrofit2.OkHttpCall.enqueue(OkHttpCall.java:52)at retrofit2.ExecutorCallAdapterFactory $ ExecutorCallbackCall.enqueue(ExecutorCallAdapterFactory.java:57)at orbyt.project.MyFragment $ 1.run(MyFragment.java:93)

这是什么问题?

作为奖励:什么是更好的方法来处理这个?我会在每次更新时更新地图.我正在考虑尝试使用Rx,但不确定这是否是一个合适的用例,或者如何实现它.

android retrofit okhttp rx-android retrofit2

23
推荐指数
1
解决办法
8231
查看次数

如何从assets文件夹加载视频?(用VideoView播放)

我需要从assets文件夹打开一个mp4视频,然后用VideoView播放它.

我尝试了这两个选项,但它们都不起作用....

mVideoView.setVideoPath("file:///android_asset/videos.mp4");
mVideoView.requestFocus();
mVideoView.start();
Run Code Online (Sandbox Code Playgroud)

和...

String uriPath = "file:///android_asset/videos.mp4";
Uri uri = Uri.parse(uriPath);
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();
Run Code Online (Sandbox Code Playgroud)

这些选项不起作用,但是如果我尝试从SDCARD打开视频它运行正常,那么问题是当我尝试从assets文件夹加载视频时.

我做错了什么?

谢谢

mp4 android assets

21
推荐指数
3
解决办法
3万
查看次数