android-layout 相关问题

布局定义用户界面的可视结构,例如活动,片段或应用程序窗口小部件的UI。

创建一个水平和垂直滚动的RecyclerView

在过去的几周里,我一直在学习使用 RecyclerView。我需要实现一个水平列表,即通过将设备切换为横向模式,如下所示: 我找到了最好的解决方案...

回答 3 投票 0

xml 预览在 android studio 中不起作用

在 Android Studio Dolphin | 2021.3.1 Patch 1,xml预览不适用于特定布局。有什么解决方案可以解决这个问题吗?请帮我解决问题。

回答 1 投票 0

如何在以编程方式滚动时禁用 tabLayout 滚动动画?

当活动开始时,我以编程方式更改选项卡布局的位置,但在以参数方式更改选项卡布局位置时,它会提供从第 0 个选项卡到我所在的选项卡的滚动动画

回答 2 投票 0

Android - Recyclerview mHasFixedSize 的默认值

我理解 mHasFixedSize 字段的概念,但是,我不确定它的默认值是什么(true 或 false)。我可以创建一个简单的 PoC 并进行测试,但是: 在这里找到答案可以帮助...

回答 1 投票 0

如何在android中实现垂直选项卡布局?

我正在尝试在平板电脑上运行的应用程序上实现垂直选项卡布局。我尝试将 TabLayout 的方向设置为垂直,但没有成功。 我正在尝试在平板电脑上运行的应用程序上实现垂直选项卡布局。我尝试将 TabLayout 的方向设置为垂直,但没有成功。 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"> <com.google.android.material.tabs.TabLayout android:id="@+id/staff_menu_tabs" android:layout_height="match_parent" android:layout_width="wrap_content" android:orientation="vertical" app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent"> <com.google.android.material.tabs.TabItem android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Tab 1"/> <com.google.android.material.tabs.TabItem android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Tab 2"/> <com.google.android.material.tabs.TabItem android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Tab 3" /> </com.google.android.material.tabs.TabLayout> </androidx.constraintlayout.widget.ConstraintLayout> 我使用以下代码制作了垂直 tabLayout。 <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:rotation="-90" android:transformPivotX="0dp" android:layout_marginTop="@dimen/tab_width" android:layout_marginLeft="-24dp"> <com.google.android.material.tabs.TabLayout android:layout_width="@dimen/tab_width" android:layout_height="wrap_content" android:background="@color/black" app:tabTextColor="@color/light_grey" app:tabSelectedTextColor="@color/white" app:tabIndicatorHeight="2dp" app:tabIndicatorColor="@color/white" app:tabPaddingBottom="-10dp"> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ABC"/> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="XYZ"/> </com.google.android.material.tabs.TabLayout> </androidx.constraintlayout.widget.ConstraintLayout> 这是使用 Jetpack Compose 实现的 Vertical-TabLayout 库 https://github.com/mohdDanishCode/MDVerticalTabLayout

回答 2 投票 0

方向改变时保存scrollview的位置

这是我的布局: 当方向改变时我需要保存滚动位置。例如,如果屏幕在纵向模式下显示从中间名开始的布局,它应该从相同的开始......

回答 7 投票 0

如何将焦点从 RowsSupportFragment 的行移动到下一个布局组件?

我正在使用 Android Leanback 库来创建电视应用程序。我有一个扩展 Fragment (即 RowsSupportFragment)的布局,其下方有一个按钮。所以,当我向下滚动到 lis 时...

回答 3 投票 0

折叠工具栏布局的渲染问题

我有一个项目,其中一个片段使用折叠工具栏布局。 直到最近,一切都很好。但上次我在 Android Studio 中遇到了折叠工具栏渲染的问题

回答 1 投票 0

如何在Android日历视图中为日期添加自定义颜色?

目前,我正在开发一个用于考勤管理系统的Andriod应用程序。用于向用户显示特定日期的出席情况,无论他们是“出席”还是“缺席”,通过

回答 1 投票 0

将一个RecyclerView插入到另一个RecyclerView中

我正在创建一套 5 张卡片,其风格为 Google Now Cards。我首先关注总体布局。 我正在使用 CardView 和 RecyclerView,我想要实现的目标是......

回答 2 投票 0

Android 在运行时将数据加载到表格布局

在我的 Android 应用程序中,我必须在表格布局中显示从数据库获取的数据。我获取数据,当我将 TableRows 添加到表中时,它给出了一个空指针。我正确实例化了......中的元素

回答 1 投票 0

回收视图CardView长按删除

在我的应用程序中,我有一个recycleView,其中有许多cardView控件。我想允许用户长按时选择 cardView 控件。在此操作中,用户可以长按操作...

回答 2 投票 0

如何使文本右对齐?

我在一列中有两个文本,我希望第二个文本右对齐: 第一的 第二 我怎样才能做到这一点? 我尝试将第二个文本包装在 Ro 中...

回答 1 投票 0

以编程方式设置 android:windowIsTranslucent

按照本教程http://cases.azoft.com/android-tutorial-floating-activity/后,我能够创建浮动活动 但是,为此,我必须在 styles.xml 中添加这一行: 按照本教程后,我能够创建浮动活动http://cases.azoft.com/android-tutorial-floating-activity/ 但是,为此,我必须在 styles.xml 中添加这一行: <item name="android:windowIsTranslucent">true</item> 仅使用 Android/Java 代码是否可以达到相同的效果? (例如在 Activity.onAttachedToWindow() 左右...) 预先感谢您的帮助。 [编辑 01] styles.xml 不得更改(而且我不应该知道其中有什么......)。但出于测试目的,我使用默认的: <resources> <style name="AppBaseTheme" parent="Theme.AppCompat.Light"> </style> <style name="AppTheme" parent="AppBaseTheme"> </style> </resources> [编辑02]Resources.Theme.applyStyle()似乎做了我想做的事情(根据API描述:“将新属性值放入主题”)。 所以我创建了以下custom_style.xml: <resources> <style name="MyCustomStyle" > <item name="android:windowIsTranslucent">true</item> </style> </resources> 然后,在onAttachedToWindow(),我打电话: getTheme().applyStyle(R.style.MyCustomStyle, true); 但是没有任何效果... 仅使用Android/Java代码是否可以达到相同的效果? 恐怕不行。您只需在 styles.xml 内完成即可。据我所知 android:windowIsTranslucent 的值无法通过编程单独更改。 当我们调用 super.onCreate(savedInstanceState); 时,Activity 类提供了一个空的图形窗口,我们可以在其中设置内容,即视图。并将主题应用于该窗口,然后将内容加载到该视图上。 因此顺序将是, 致电super.onCreate() 设置活动主题。 设置该活动的内容视图。 例如。 styles.xml <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> <item name="android:windowBackground">@drawable/background</item> <item name="android:windowNoTitle">true</item> </style> <!-- Application theme.without title bar --> <style name="AppTheme.NoTitleBar" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> <item name="android:windowBackground">@drawable/background</item> <item name="android:windowNoTitle">true</item> </style> <!--Floating activity theme --> <style name="Theme_Translucent" parent="android:style/Theme.NoTitleBar.Fullscreen"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowFrame">@null</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:backgroundDimEnabled">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <item name="android:windowFullscreen">true</item> </style> 然后为Floating activity设置主题,如下所示: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(R.style.Theme_Translucent); // Set here setContentView(...) } 这样做 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window w = getWindow(); // in Activity's onCreate() for instance w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } Android R 出现新方法 不能说要以编程方式执行此操作..但如果您需要执行此操作,您可以像这样尝试.. 将主题添加到清单文件中的活动中,如下所示.. android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" 示例:- <activity android:name="com.example.androidhackerphonelocker.MainActivity" android:label="@string/app_name" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 希望它能解决您的一些问题.. 也许这会有所帮助。 @Override protected void onCreate(Bundle savedInstanceState) { processSetTheme(this); getWindow().requestFeature(Window.FEATURE_NO_TITLE); getWindow().setBackgroundDrawableResource(R.color.realTranslucent); if (Build.VERSION.SDK_INT>=19){ getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } super.onCreate(savedInstanceState); <color name="realTranslucent">#00000000</color> 如果 android.app.Dialog window.setDimAmount(0f) window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) 从 API 级别 30 开始,现在有一个 setTranslucent 方法: 将活动(尤其是具有 R.attr.windowIsTranslucent 或 R.attr.windowIsFloating 属性的活动)转换为全屏不透明活动,或将其从不透明转换回半透明。 文档

回答 7 投票 0

Android:ScrollView 无法在键盘退出时滚动

我有一个带有一些视图的布局,其中一个是 EditText。布局很容易适合一页,但是,当软键盘退出时,布局不会滚动。 这是我的布局的回顾: <...

回答 12 投票 0

android 工具栏和底栏在片段容器视图中重叠回收器视图

我尝试将recyclerview与自定义工具栏和底栏一起使用,但两者都是重叠的recyclerview,即使我在xml中使用了constraintTop_toBottomOf和constraintBottom_toTopOf ...

回答 1 投票 0

添加 navGraph 属性时,膨胀类 Fragment 时出错

我知道这个问题有一些问题和答案,但没有一个解决方案能解决我的问题。我得到一个 android.view.InflateException:二进制 XML 文件行 #11:二进制 XML 文件行 ...

回答 4 投票 0

CoordinatorLayout + AppBarLayout + NavigationDrawer

将 CoordinatorLayout 与 AppBarLayout 和 NavigationDrawer 组合时遇到布局问题。 问题是,NavigationDrawer 及其内容隐藏在工具栏后面。 ...

回答 3 投票 0

为什么 Android 自定义对话框会全屏显示

我正在尝试创建自定义对话框。 基本布局(我也尝试过各种修改后的布局): 我正在尝试创建自定义对话框。 基本布局(我也尝试过各种修改后的布局): <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:background="@color/dialogHeaderBackground" android:gravity="center" android:minHeight="@dimen/minHeightDialogTitle" android:text="" android:textStyle="bold" /> <TextView android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/title" android:layout_marginTop="5dp" android:text="" /> <LinearLayout android:id="@+id/loutButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_below="@+id/message" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:orientation="horizontal" > <Button android:id="@+id/btnNeutral" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="" /> <Button android:id="@+id/btnNegative" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="" /> <Button android:id="@+id/btnPositive" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="" /> </LinearLayout> </RelativeLayout> Java代码: Dialog dialog = new Dialog(this); // Dialog dialog = new Dialog(this,R.style.Theme_Dialog); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.custom_dialog); Button btnPositive=(Button) dialog.findViewById(R.id.btnPositive); Button btnNegative=(Button) dialog.findViewById(R.id.btnNegative); Button btnNeutral=(Button) dialog.findViewById(R.id.btnNeutral); TextView txvTitle=(TextView)dialog.findViewById(R.id.title); TextView txvMessage=(TextView)dialog.findViewById(R.id.message); ... so on and so forth 我收到以下对话框,该对话框是全屏的,但我不想要: 已经尝试过的解决方案,其结果与上面的屏幕截图相同: Styles.xml 配置 1 | 实现了 Pastebin 代码 Styles.xml 配置 2 | 实现了 Pastebin 代码 Android 开发指南中提供的使用 AlertDialog.Builder 的示例 | 实现的Java代码 我的底层布局都没有使用 MATCH_PARENT 的高度。正如此堆栈溢出中所述 answer 还尝试将窗口设置更改为WRAP_CONTENT,以根据答案此处 | 进行对话在 Pastebin 中实现 Java 代码 还尝试将整个RelativeLayout保留在父级LinearLayout中,请参阅下面的答案中的第3条评论 我唯一能得到像样的对话框是在指定 XML 布局的高度时,就像这样 android:layout_height="150dp" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="150dp" > 指定固定高度时的对话框: 当消息较大时,内容/布局会丢失 当消息较小时,对话框显示得比需要的大,请参见下面的“确定”按钮 不要建议上述(指定固定高度)作为解决方案,因为它效率不高,看起来不太好,也不是动态的,它是静态的。 要指定什么,看起来不错的自定义对话框,也可以自定义? 从线性布局中删除android:layout_alignParentBottom="true"线 <LinearLayout android:id="@+id/loutButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_below="@+id/message" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:orientation="horizontal" > 从上面的代码中删除android:layout_alignParentBottom="true"并检查。 是的,它发生在相对布局中。 在其谷歌docs中明确提到。 请注意,大小之间不能存在循环依赖关系 relativelayout 及其子级的位置。例如,你 不能有高度设置为 WRAP_CONTENT 的relativelayout 和 子项设置为 ALIGN_PARENT_BOTTOM 当您使用对话框时,您必须根据您的要求使用线性布局。 它绝对会为你工作。 进一步的想法可能不适合你,但我只是提一下供参考 您可以使用java代码来制作固定大小的对话框, 或者您可以从布局参数设置布局边距,以便在不同的屏幕尺寸下保持响应能力。 你还可以做一件事, 获取屏幕尺寸 DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int height = metrics.heightPixels; int width = metrics.widthPixels; 你可以计算新的对话框大小并将其设置为@guobao的答案 DisplayMetrics displayMetrics = new DisplayMetrics(); dialog.getWindow().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); WindowManager.LayoutParams params = dialog.getWindow().getAttributes(); params.width = (new width); params.height = (new height); 询问我是否需要任何进一步的规格。 您可以在对话框中设置此代码 DisplayMetrics displayMetrics = new DisplayMetrics(); dialog.getWindow().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); WindowManager.LayoutParams params = dialog.getWindow().getAttributes(); params.width = (the width you want to set); params.height = (the height you want to set); 这是我遇到同样问题时的方法。 我扩展了 DialogFragment(来自 support.v4 lib) 然后充气: @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); final View view = inflater.inflate(R.layout.dialog_whatever, null); builder.setView(view); return builder.create(); } 这是布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/rounded_corners" android:padding="20dp"> <TextView android:id="@+id/confirm_dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:textColor="@color/black" android:text="@string/are_you_sure_declining_whatever"/> <LinearLayout android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|bottom" android:orientation="horizontal"> <Button android:id="@+id/cancel_action" android:layout_marginRight="10dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/transparent" android:text="@string/cancel" android:textColor="@color/accent_color"/> <Button android:id="@+id/confirm_action" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/transparent" android:textColor="@color/accent_color" android:text="@string/reject"/> </LinearLayout> </LinearLayout> 这种方法也遵循材料设计指南! 希望对您有帮助 致以诚挚的问候, 佩德罗维奇90

回答 5 投票 0

将项目符号与包装的 TextView 的第一行对齐

我正在尝试在 Android 中创建一个项目符号列表,其中项目符号与 TextView 中第一行的垂直中心对齐,如下所示: 单个项目符号/文本行的 XML 如下: 我正在尝试在 Android 中创建一个项目符号列表,其中项目符号与 TextView 中第一行的垂直中心对齐,如下所示: 单个项目符号/文本行的 XML 是这样的: <android.support.constraint.ConstraintLayout android:id="@+id/setup_intro_bullet_first_container" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"> <TextView android:id="@+id/setup_intro_bullet_first" style="@style/TextAppearance.AppCompat.Headline" android:layout_width="0dp" android:layout_height="wrap_content" android:text="@string/bullet" android:textColor="@color/colorAccent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/setup_intro_bullet_first_text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="4dp" android:paddingTop="14dp" android:text="@string/setup_intro_benefit_notification" android:textColor="@android:color/white" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toRightOf="@id/setup_intro_bullet_first" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> 这里我在 TextView 上使用填充来与项目符号对齐,但这不是解决方案,因为对齐方式在其他分辨率中不一致。我还尝试将项目符号字符放入文本字符串本身中,但第二行文本将位于项目符号下方,这不是我想要的。我想要一些关于如何实现这一目标的建议。 我想出的解决方案是创建一个圆圈Drawable并在ImageView中使用它。您可以定义 ImageView 的基线,这使我能够将其与 TextView 的基线正确对齐。这是圆 XML: <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="@color/colorAccent" /> </shape> 以及布局 XML: <ImageView android:id="@+id/setup_intro_bullet_first" style="@style/TextAppearance.AppCompat.Headline" android:layout_width="4dp" android:layout_height="4dp" android:baseline="7dp" android:src="@drawable/circle" app:layout_constraintBaseline_toBaselineOf="@+id/setup_intro_bullet_first_text" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@+id/setup_intro_bullet_first_text" /> <TextView android:id="@+id/setup_intro_bullet_first_text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="4dp" style="@style/TextAppearance.AppCompat.Subhead" android:text="@string/setup_intro_benefit_notification" android:textColor="@android:color/white" app:layout_constraintLeft_toRightOf="@id/setup_intro_bullet_first" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> 使用 app:layout_constraintBaseline_toBaselineOf 属性将“项目符号”视图的基线限制为文本视图的基线。 项目符号视图的限制: app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@+id/setup_intro_bullet_first_text" app:layout_constraintBaseline_toBaselineOf="@+id/setup_intro_bullet_first_text" 您的文字: app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toRightOf="@id/setup_intro_bullet_first" app:layout_constraintRight_toRightOf="parent" app:layout_constraintBottom_toBottomOf="parent" 对于 Android Pie (28) 及以上版本,您可以直接使用 BulletSpan。对于以下版本,您可以使用 DrawableMarginSpan,但带有损坏的可绘制加项目符号 "\u2022"。 val yourSpan = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { BulletSpan( 8.px, ContextCompat.getColor(requireContext(), R.color.gray), 2.px, ) } else { DrawableMarginSpan(ContextCompat.getDrawable(requireContext(), R.drawable.ic_circle)!!, 20) } val yourString = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { "Just string" } else { "\u2022" + " Just string" } yourTextView.text = SpannableStringBuilder(yourString).apply { setSpan( yourSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ) } 损坏的可绘制代码: <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="@color/gray"/> </shape>

回答 3 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.