小编Mac*_*ver的帖子

如何在字符串资源中使用切换大小写

如何将切换案例方案与字符串资源而不是硬编码名称一起使用?我的字符串与它们的拼写完全相同。

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            MainListAdapter adapter = (MainListAdapter) parent.getAdapter();
            Main continent = adapter.getItem(position);
               if (mTwoPane) {
                    View rowView = view;
                    setItemSelected(continent, rowView);

                    Fragment newFragment;
                    switch (stringRes) {
                        case R.id.africa:
                            newFragment = new FragmentAfrica();
                            break;
                        case R.id.asia:
                            newFragment = new FragmentAsia();
                            break;
                        case R.id.europe:
                            newFragment = new FragmentEurope();
                            break;
                        default:
                            newFragment = new FragmentAfrica();
                    }

                    MainActivity activity = (MainActivity) view.getContext();
                    FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.detail_container, newFragment);
                    transaction.commit();
                } else {
              }
Run Code Online (Sandbox Code Playgroud)

java android switch-statement android-fragments

5
推荐指数
1
解决办法
6343
查看次数

如何计算布局中的视图数量

Android Studio 有没有办法显示 XML 布局中存在的视图数量?众所周知,布局应该包含 <=80 个视图,因此超过这个数量就会出现此警告,因此告知数量将非常有帮助。

LayoutName.xml 有 80 多个视图,对性能不利

public class FragmentApple extends android.support.v4.app.Fragment {

    public FragmentApple () {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_apple,container,false);

        int num = FragmentApple.getChildCount(); Log.d("Number of views in layout: ", "" + num);

        return v;
    }
}
Run Code Online (Sandbox Code Playgroud)

xml android android-layout android-view android-studio

5
推荐指数
1
解决办法
5962
查看次数

表格行中的多行文本视图未正确包装

我试图让我的文本视图文本在表格行中显示在多行上,但由于某种原因它没有正确包装,我最终得到了这个(见截图).我知道文本视图有一些事情要做,但我不知道是什么导致了这个问题.如何解决此问题?

截图

在此输入图像描述 在此输入图像描述

列表项XML

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableRow>
        <ImageView
            android:id="@+id/img"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_vertical" />

        <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:singleLine="false"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>
</TableLayout>
Run Code Online (Sandbox Code Playgroud)

xml android textview android-tablelayout

5
推荐指数
1
解决办法
4606
查看次数

在平板电脑上列表滚动后意外重复使用所选列表项背景颜色

对于我在平板电脑上的列表视图,我试图让我选择的列表项选择在选中时保持其状态但不幸的是我看到一些奇怪的行为.出于某种原因,每当我滚动列表到所选项目不可见的点,然后滚动回到所选项目可见的点时,背景颜色意外地被重用.我相信有些东西需要放在getView方法中,但我不知道如何处理这个方法.必须采取哪些措施来防止背景颜色被重复使用?

适配器类

public class VictoriaListAdapter extends BaseAdapter {

    private List<Victoria> mData;
    private LayoutInflater mInflater;

    public VictoriaListAdapter (List<Victoria> data, Context context) {
        mData = data;
        mData = new ArrayList(mData);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int position) {
        return mData.get(position).getStation();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item_dualline, parent, …
Run Code Online (Sandbox Code Playgroud)

java xml android android-selector android-5.0-lollipop

5
推荐指数
1
解决办法
264
查看次数

ViewPager2 - 如何使用 registerOnPageChangeCallback 检查视图分页器项目位置

如何使用 ViewPager2 的registerOnPageChangeCallback功能来检查当前可见的 ViewPAger2 片段的位置?if我知道这将由/语句组成when,但不知道实现它的正确方法。

mViewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                if (mViewPager.currentPage) == 0 {
                // do something
                }
                else if (mViewPager.currentPage) == 1 {
                // do something
                }
                else {
                // do something
                }
                super.onPageSelected(position)
            }
        })
Run Code Online (Sandbox Code Playgroud)

android android-viewpager kotlin android-viewpager2

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

使用AppCompat以编程方式更改操作栏背景和文本颜色

我正在尝试使用AppCompat以编程方式更改操作栏的背景颜色和文本颜色.这是我之前使用Holo主题时使用的代码,但似乎我不能对AppCompat使用相同的东西.有谁知道我需要改变什么?

ActionBar bar = getActionBar();
    bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#95CDBA")));
    getActionBar().setTitle(Html.fromHtml("<font color='#000099'>Hello World</font>"));
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

java android

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

避免将null作为视图根传递(在AlertDialog中扩大自定义布局时)

试图为AlertDialog添加自定义布局,但请注意这一点。我已经看到了几种不同的解决方案,但是不知道哪种方案对我来说是正确的。摆脱此空警告的实际正确方法是什么?

避免将null用作视图根(需要解析膨胀的布局的根元素上的布局参数)

@Override
public void onClick(View v) {
  AlertDialog alertDialog = new 
  AlertDialog.Builder(getActivity()).create();

  LayoutInflater inflater = getActivity().getLayoutInflater();
  View content = inflater.inflate(R.layout.dialog_customd, null);
  alertDialog.setView(content);

  alertDialog.show();
}
Run Code Online (Sandbox Code Playgroud)

android android-layout layout-inflater android-alertdialog

4
推荐指数
2
解决办法
2055
查看次数

如何在没有支持库的情况下将MaterialToolbar设置为ActionBar

有没有办法将 a 设置MaterialToolbarActivityan ActionBarToolbar每当我尝试设置我的时,Android Studio 都会不断向支持库请求。

必需:工具栏?- 找到:材质工具栏

XML

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/myAppBarLayout">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:id="@+id/myCollapsingToolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|snap|exitUntilCollapsed">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/myToolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:contentInsetStartWithNavigation="0dp"
            app:layout_collapseMode="pin" />
    </com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)

科特林

class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.collapsingtoolbar_and_recyclerview)

        val mCoordinatorLayout = findViewById<CoordinatorLayout>(R.id.myCoordinatorLayout)
        val mCollapsingToolbarLayout = findViewById<CollapsingToolbarLayout>(R.id.myCollapsingToolbarLayout)
        val mToolbar = findViewById<MaterialToolbar>(R.id.myToolbar)

        // ...Do other stuff here
        setActionBar(mCollapsingToolbarLayout)
        mCollapsingToolbarLayout.title = "Hello, world!"
    }
}
Run Code Online (Sandbox Code Playgroud)

android android-activity kotlin android-actionbar android-toolbar

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

如何在Kotlin中将Any转换为List?

当我尝试投AnyList像在下面的例子中,我得到" Unchecked cast: Any! to List"的警告.有这种问题的解决方法吗?

val x: List<Apples> = objectOfTypeAny as List<Apples>
Run Code Online (Sandbox Code Playgroud)

kotlin

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

Java 8:如何使用设备设置的语言环境解析DayOfWeek

为什么每当我设置DayOfWeek为我的TextView输出总是出现在大写字母?另外,更改设备语言后,日子永远不会改变。如何将其设置为根据设备的当前语言环境进行更改?

    myTv.text = DayOfWeek.MONDAY.toString()
Run Code Online (Sandbox Code Playgroud)

输出量

MONDAY
Run Code Online (Sandbox Code Playgroud)

java dayofweek java-8 kotlin

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