【问题标题】:android - How to change textview maxLines in orientation change?android - 如何在方向更改中更改 textview maxLines?
【发布时间】:2018-07-23 23:34:43
【问题描述】:

如何改变方向模式下的textview maxLines?例如,在纵向模式下 4 maxlines,但在横向模式下 2 maxlines。我尝试使用下面的代码,但没有成功,仅在纵向模式下显示 4 行,但在横向不显示 2 行:

主活动

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          maxlines();
             case...
             default:
                        break;
                }

.......................

private void maxlines(){
        if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            arabicfont.setMaxLines(4);
        }else{
            if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
                arabicfont.setMaxLines(2);
            }
        }
    }

清单

<activity
            android:name=".MainActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

【问题讨论】:

  • 在巡演活动中覆盖 onConfigurationChanged 并将 maxlines 放在上面
  • 再次提问。自第一次启动应用程序以来,如何将屏幕识别为肖像?因为,当第一次启动应用程序并单击 listview 项目时,它没有显示 4 条最大线(它填充所有布局),但在横向然后返回纵向它显示 4 条线
  • @alizulfuqar 从 onResume() 方法调用 maxlines() 函数。
  • 我想添加到 onCreate 而不是 onResume。因为简历是简历,所以我要启动应用程序
  • @alizulfuqar 请参考我在回答部分添加的生命周期图表。 onResume 也会在 onCreate 启动时启动。

标签: android textview landscape-portrait screen-rotation


【解决方案1】:

这个方法有两种解决方法:

1) 重写 OnConfigurationChanged 方法:当设备方向改变时调用该方法。

2) 通过从该方法调用 maxlines() 函数来覆盖 OnResume 方法:该方法在整个活动 lifecycle 中被调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-18
    • 2011-01-19
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多