Android 在自定义首选项中打开带有微调器时间选择器的自定义对话框会导致滚动条出现在时间选择器中
我在微调器模式下有一个带有时间选择器的自定义对话框,我在首选项屏幕中使用自定义首选项显示该对话框。当“自定义”对话框弹出时,在微调器模式时间选择器附近会出现一个滚动条。然后,此滚动条将淡出。()这仅在从首选项屏幕(自定义首选项)显示自定义对话框时发生。我在应用程序的其他地方也使用过这个对话框,效果很好 我如何阻止它出现 附加下面的所有自定义类 SettingsActivity.javaAndroid 在自定义首选项中打开带有微调器时间选择器的自定义对话框会导致滚动条出现在时间选择器中,android,dialog,timepicker,android-timepicker,Android,Dialog,Timepicker,Android Timepicker,我在微调器模式下有一个带有时间选择器的自定义对话框,我在首选项屏幕中使用自定义首选项显示该对话框。当“自定义”对话框弹出时,在微调器模式时间选择器附近会出现一个滚动条。然后,此滚动条将淡出。()这仅在从首选项屏幕(自定义首选项)显示自定义对话框时发生。我在应用程序的其他地方也使用过这个对话框,效果很好 我如何阻止它出现 附加下面的所有自定义类 SettingsActivity.java package com.example.testing; import android.os.Bundle;
package com.example.testing;
import android.os.Bundle;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}
root_preferences.xml
<PreferenceScreen>
<com.example.testing.CustomPreferenceCategory>
</com.example.testing.CustomPreferenceCategory>
</PreferenceScreen>
自定义\首选项\类别\布局.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="12dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="TEST"
android:textSize="22sp"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="right|center_vertical"
android:layout_weight="0"
android:id="@+id/button1"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right">
<Button
android:id="@+id/ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:id="@+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cancel" />
</LinearLayout>
</LinearLayout>
自定义对话框布局.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="12dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="TEST"
android:textSize="22sp"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="right|center_vertical"
android:layout_weight="0"
android:id="@+id/button1"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right">
<Button
android:id="@+id/ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:id="@+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cancel" />
</LinearLayout>
</LinearLayout>
如果你知道如何用数字打卡来修复它,那么使用计时器也应该很容易。
它们由2个线性布局、3个数字图标和1个MaterialTextView组成。
您可以使用getChild()方法循环遍历它们,并使用instanceof检查您所处的位置
private void loopview(视图组视图){
for(int i=0;i
如果你知道如何用数字打卡来修复它,那么使用计时器也应该很容易。它们由2个线性布局、3个数字图标和1个MaterialTextView组成。
您可以使用getChild()方法循环遍历它们,并使用instanceof检查您所处的位置
private void loopview(视图组视图){
for(int i=0;i
这就是最终删除滚动条的原因
(((LinearLayout) ((LinearLayout) timePicker.getChildAt(0)).getChildAt(0)).getChildAt(0)).setVerticalScrollBarEnabled(false);
(((LinearLayout) ((LinearLayout) timePicker.getChildAt(0)).getChildAt(0)).getChildAt(2)).setVerticalScrollBarEnabled(false);
放在CustomDialog.java中
package com.example.testing;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class CustomDialog extends Dialog {
public CustomDialog(@NonNull Context context) {
super(context);
}
public CustomDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
protected CustomDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialog_layout);
Button okButton = (Button) findViewById(R.id.ok_button);
Button cancelButton = (Button) findViewById(R.id.cancel_button);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
}
这将专门将计时器选择器中的小时和分钟数字图标的滚动条设置为“无”
感谢@einUsername为我指明了正确的方向。这就是最终删除滚动条的原因
(((LinearLayout) ((LinearLayout) timePicker.getChildAt(0)).getChildAt(0)).getChildAt(0)).setVerticalScrollBarEnabled(false);
(((LinearLayout) ((LinearLayout) timePicker.getChildAt(0)).getChildAt(0)).getChildAt(2)).setVerticalScrollBarEnabled(false);
放在CustomDialog.java中
package com.example.testing;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class CustomDialog extends Dialog {
public CustomDialog(@NonNull Context context) {
super(context);
}
public CustomDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
protected CustomDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialog_layout);
Button okButton = (Button) findViewById(R.id.ok_button);
Button cancelButton = (Button) findViewById(R.id.cancel_button);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
}
这将专门将计时器选择器中的小时和分钟数字图标的滚动条设置为“无”
感谢@einUsername为我指明了正确的方向。它是立即消失还是如何消失?也许可以尝试对自定义对话框布局进行约束调整,并暂时删除按钮以查看是否有任何变化。@einUsername在问题中添加了淡入灰色区域的gif。还尝试了约束布局和删除按钮,但没有成功。只是观察:这些线条看起来像滚动条。它们以大约相同的速度衰减,第二个更小是有道理的,因为它有更多的数字。“首选项”活动可能会更改滚动条样式。或者你也可以试试普通的数字图标。@einUsername看起来像是一个滚动条。!!!!我放了一个数字图标,同样的现象也发生了。我可以从NumberPicker中删除滚动条,但时间选择器没有滚动条属性。已尝试使用许多属性,但没有更改计时器滚动条。我通过将灰色区域更改为滚动条更新了问题。它是立即消失还是如何淡出?也许可以尝试对自定义对话框布局进行约束调整,并暂时删除按钮以查看是否有任何变化。@einUsername在问题中添加了淡入灰色区域的gif。还尝试了约束布局和删除按钮,但没有成功。只是观察:这些线条看起来像滚动条。它们以大约相同的速度衰减,第二个更小是有道理的,因为它有更多的数字。“首选项”活动可能会更改滚动条样式。或者你也可以试试普通的数字图标。@einUsername看起来像是一个滚动条。!!!!我放了一个数字图标,同样的现象也发生了。我可以从NumberPicker中删除滚动条,但时间选择器没有滚动条属性。已尝试使用许多属性,但没有更改计时器滚动条。我通过将灰色区域更改为滚动条更新了问题。时间选择器只有一个子项,这是一个线性布局。所以if(v instanceof NumberPicker)行总是false。编辑:我递归地寻找子元素,最后找到了两个timePicker元素。我想我将具体地称之为这两个元素。而不是在每件事上循环。谢谢你给我指明了正确的方向。我已经发布了我用来解决的答案,作为一个单独的解决方案,以防任何人遇到同样的问题并需要帮助。只有一个孩子的时间选择器,这是一个线性布局。所以if(v instanceof NumberPicker)行总是false。编辑:我递归地寻找子元素,最后找到了两个timePicker元素。我想我将具体地称之为这两个元素。而不是在每件事上循环。谢谢你给我指明了正确的方向。我已经发布了我用来解决的答案,作为一个单独的解决方案,以防任何人面临同样的问题,需要帮助。