小编Kar*_*uri的帖子

对齐ImageSpan中心垂直周围的文本

我有ImageSpan一段文字.我注意到的是,周围的文本总是在文本行的底部绘制 - 更准确地说,文本行的大小随着图像的增长而增加,但文本的基线不会向上移动.当图像明显大于文本大小时,效果相当难看.

这是一个示例,大纲显示的界限TextView: 在此输入图像描述

我试图让周围的文本相对于正在显示的图像垂直居中.以下是蓝色文本显示所需位置的相同示例:

在此输入图像描述

以下是我受约束的约束:

  • 我不能使用复合drawables.图像必须能够在单词之间显示.
  • 根据内容,文本可以是多行的.我无法控制这一点.
  • 我的图像比周围的文字大,我不能减小它们的大小.虽然上面的示例图像大于实际图像(以演示当前行为),但实际图像仍然足够大,以至于此问题显而易见.

我已尝试android:gravity="center_vertical"在TextView上使用该属性,但这没有任何效果.我认为这只是文本行的垂直居中,但在文本行中,文本仍然在底部绘制.

我目前的思路是创建一个自定义跨度,根据行的高度和当前文本大小来移动文本的基线.这个跨度将包含整个文本,我将不得不计算与ImageSpans 的交集,所以我也可以避免移动图像.这听起来相当令人生畏,我希望有人能提出另一种方法.

任何和所有的帮助表示赞赏!

android text spannable imagespan

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

ListPopupWindow不遵守WRAP_CONTENT宽度规范

我正在尝试使用ListPopupWindow来显示一个字符串列表ArrayAdapter(最终这将是一个更复杂的自定义适配器).代码如下.如屏幕截图所示,结果ListPopupWindow似乎表现为内容宽度为零.它显示了正确数量的项目,这些项目仍然可以点击,并且点击成功生成了一个项目Toast,因此至少可以正常工作.

一个有趣的说明:我可以提供宽度(以像素为单位)popup.setWidth(...)而不是ListPopupWindow.WRAP_CONTENT它会显示一些内容,但这似乎非常不灵活.

如何ListPopupWindow包装其内容?

测试活动:

public class MainActivity extends Activity {

    private static final String[] STRINGS = {"Option1","Option2","Option3","Option4"};
    private View anchorView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBar().setHomeButtonEnabled(true);
        setContentView(R.layout.activity_main);
        anchorView = findViewById(android.R.id.home);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                showPopup();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showPopup() {
        ListPopupWindow popup …
Run Code Online (Sandbox Code Playgroud)

user-interface android popupwindow

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

Android radiogroup,radiobuttons之间的分隔线

是否有一种简单的方法在RadioButtons内部添加分隔符RadioGroup?我尝试过使用dividerxml属性,但似乎没有用.如果它是相关的RadioGroup,我的布局中不包含任何子视图; 我正在以RadioButtons编程方式添加.

编辑:问题解决了.您可以在xml中添加RadioButton内部视图RadioGroup.在我的情况下,您也可以以编程方式执行此操作,但请注意您的布局参数.Akki有正确的想法,这对我有用:

for (int i = 0; i < items.size(); i++) {
    if (i > 0) {
        // add a divider with height of 1 pixel
        View v = new View(this);
        v.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.MATCH_PARENT, 1));
        v.setBackgroundColor(android.R.color.darker_gray);
        mRadioGroup.addView(v);
    }
    RadioButton rb = new RadioButton(this);
    /* set other properties ... */

    mRadioGroup.addView(rb);
}
Run Code Online (Sandbox Code Playgroud)

android radio-group radio-button divider

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

为什么我在RecyclerView上获得空引用

我试图在我的片段中使用RecyclerView.它显示第一个选项卡正常,但是当我滑动到第二个选项卡然后回到第一个选项卡时,我收到以下错误:

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.support.v7.widget.RecyclerView $ LayoutManager.stopSmoothScroller()'

有谁知道为什么我会收到这个错误?似乎在片段切换之前有一个我失踪的电话,但我无法理解.

用于片段的寻呼机适配器:

public class PagerAdapter extends FragmentPagerAdapter {

    private final String[] TITLES = {"Header 0", "Header 1" };

    public PagerAdapter(FragmentManager fm){
        super(fm);
    }

    @Override
    public CharSequence getPageTitle(int position){
        return TITLES[position];
    }

    @Override
    public int getCount() {
        return TITLES.length;
    }

    @Override
    public Fragment getItem(int position){
        switch(position){
            case 0:
                return new FragmentOne();
            case 1:
                return new FragmentTwo();
            default:
                return new FragmentOne();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

FragmentOne&FragmentTwo使用我创建的样板类:

public class FragmentOne extends Base {
    ... code for displaying content in …
Run Code Online (Sandbox Code Playgroud)

java android android-fragments

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

显示工具栏下的内容

您好我试图简单地将我的内容放在工具栏下面,但是当我运行我的应用程序时,当它应该低于它时,一些内容隐藏在它后面.

我已经阅读了关于使用框架布局来尝试将其分开但我已经陷入困境.我目前正在使用随软件提供的基本android studio导航抽屉模板,并想知道我必须做出哪些更改.

我的协调员布局

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

我的抽屉布局

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

  </android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

我需要做出哪些改变?

xml android toolbar android-layout android-toolbar

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

在Android上构建兼容性PreferenceFragment

我的android应用程序使用兼容性库(Loaders特别是).我需要能够在任何地方使用兼容性组件,包括我的PreferenceActivity.遗憾的是,支持库不包括基于片段的类PreferenceActivity,也不包括PreferenceFragment类.

我想要创建自己的PreferenceFragmentActivity和CompatPreferenceFragment(基本上是谷歌忽略的).其他人,包括StackOverflow上的一些人,已经提到完成这个确切的事情,所以我公开寻求任何形式的帮助 - 指导,提示,建议,代码示例(如果您愿意并且能够分享),或者您认为的任何其他内容与这个问题有关.

如果有人也可以将我引导到人们提交Android功能请求的网站,我想将我的名字添加到希望Google在支持库的未来版本中解决此问题的人员列表中.

compatibility android preferenceactivity android-fragments

14
推荐指数
1
解决办法
6134
查看次数

屏幕旋转后Android片段重叠

这个应用程序是我使用碎片等学习练习.它有一个活动来管理一些不同的片段.第一个是ListFragment(A),它显示应用程序何时启动并从游标加载列表项.单击列表项会通知活动将ListFragment替换为该项目的DetailFragment(B).如果在看(B)的同时旋转屏幕,我会看到(B)在(A)之上,如下所示:

http://postimage.org/image/uhy016ds7/

我最好的猜测是它与Android销毁和重新创建配置更改活动有关.我想知道什么是这个问题最干净的解决方案.下面是一些代码片段,请告诉我是否应该发布其他内容.

RES /布局/ home.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <fragment
        android:id="@+id/fragment_place_list"
        android:name="com.simbiosys.apps.foodfast.ui.PlaceListFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </fragment>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

MyActivity.onCreate

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    placeListFragment = (PlaceListFragment) getSupportFragmentManager().findFragmentById(
            R.id.fragment_place_list);

}
Run Code Online (Sandbox Code Playgroud)

MyActivity.showDetailPane

public void showDetailPane(String id, String reference) {
    placeDetailFragment = PlaceDetailFragment.newInstance(id, reference);
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.addToBackStack(null);
    ft.hide(placeListFragment);
    ft.replace(R.id.fragment_container, placeDetailFragment);
    ft.show(placeDetailFragment);
    ft.commit();
}
Run Code Online (Sandbox Code Playgroud)

android fragment screen-rotation overlapping

10
推荐指数
3
解决办法
8011
查看次数

Fragment中的MapView - 指定的子节点已经有父节点

我正在尝试在片段中显示MapView(使用被黑客入侵的兼容性库).以下在过去工作得很好:

  • 片段onCreateView()只是返回一个新的FrameLayout
  • fragment onActivityCreated()从Acitivity获取MapView并将其添加到其视图层次结构中
  • onDestroyView() 从视图层次结构中删除MapView

现在我希望片段使用xml中定义的布局,这样我就可以拥有其他一些UI内容.将MapView元素放在布局文件中总是崩溃,所以我这样做:

map_screen_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <FrameLayout
        android:id="@+id/map_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </FrameLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

MapScreenActivity保持实际MapView和片段调用getMapView(),所以我不会遇到"不能有多个MapView"的问题:

MapScreenActivity.java

public class MapScreenActivity extends FragmentActivity {
    protected Fragment fragment;
    protected MapView mapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.single_pane_empty);

        if (savedInstanceState == null) {
            fragment = new MapScreenFragment();

            getSupportFragmentManager().beginTransaction().add(R.id.root_container, fragment)
                    .commit();
        }
    }

    public MapView getMapView() {
        if (mapView == null) …
Run Code Online (Sandbox Code Playgroud)

android fragment android-mapview

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

Android:自定义视图的默认属性

我有一个自定义视图,扩展了其中一个框架类.ViewAndroid中的大多数s都为它们定义了一些默认属性(例如Button可点击,由设置android:clickable="true").

如何为自定义视图提供应用程序范围的默认值?

android attributes custom-controls attr

9
推荐指数
1
解决办法
8374
查看次数

TypedArray中的getDrawable()返回null?

在我的TypedArray.getDrawable()一个自定义中,我遇到了一个奇怪的问题View.为简单起见,这是一个显示同样问题的简单测试项目:

TestView.java

package com.example.testing;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

public class TestView extends View {

    private Drawable mDrawable;

    public TestView(Context context) {
        this(context, null);
    }

    public TestView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TestView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TestView, defStyle, 0);
        try {
            Drawable d = a.getDrawable(R.styleable.TestView_testDrawable);
            if (d != null) {
                setDrawable(d);
            }
        } finally { …
Run Code Online (Sandbox Code Playgroud)

android drawable custom-component attr

9
推荐指数
1
解决办法
5233
查看次数