在出现之前测量一个recyclerView时,我目前遇到了一个问题.我需要measuredHeight才能启动"扩展"动画.
这是我之前正在处理的代码中的gridView,我正在尝试使用GridLayoutManager将其迁移到RecyclerView
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
final int heightSpec = View.MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, View.MeasureSpec.AT_MOST);
mGridView.measure(widthSpec, heightSpec);
int targetHeight = mGridView.getMeasuredHeight();
Run Code Online (Sandbox Code Playgroud)
它正在使用gridView,但是如果我在recyclerView上使用相同的度量规范调用measure方法,结果总是16777215,我认为这可能是某些东西的最大值,但我不能说什么.
我看到一些帖子解释说,可以通过使用以下测量规范测量视图,然后在屏幕上呈现视图之前测量视图:
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
final int heightSpec = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
Run Code Online (Sandbox Code Playgroud)
但是对于recyclerView.getMeasuredHeight();我得到0.
有没有办法在recyclelerView在屏幕上呈现之前正确测量它的高度?
谢谢.
我有一个附加到我的MainActivity的自定义片段.片段的布局文件包含recyclerview小部件.
fragment_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
在我的RecyclerView.Adapter中,onCreateViewHolder方法如下所示:
@Override
public MyAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout
.list_item, parent, false);
return new MyHolder(view);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是关于此方法的Viewgroup父级.这个ViewGroup是我的RecyclerView小部件,但为什么给我parent.getContext引用我的MainActivity而不是我的片段?
android android-fragments android-viewgroup android-recyclerview
因为我已经尝试过用动作拖动阅读Android文档的视图.我显然没有说过它的基础但我想隐藏或invisible查看哪些view正在dragged或正在使用的其他motion可以被带到隐藏或invisible surface区域?
AndroidViewGroup有以下方法来添加(子)视图:
我知道我们可以轻松地在 xml/programatically 中定义 LinearLayout 的水平/垂直方向并添加子视图,例如。
与 RelativeLayout 类似,我们可以使用ViewGroup'saddView和RelativeLayout.LayoutParams's 方法:
我也知道我们可以LinearLayout在里面ConstraintLayout像个孩子一样使用并玩它。
是否有任何可靠且推荐的方法将子视图动态添加到 ConstraintLayout?
更新:让我举一个不太简单的例子,说明我想要实现的目标。
假设有一个View1,View2和View3的所有V1,V2和V3被垂直对齐的一个下面的另一个并且是由多个的复杂视图TextView秒和ImageView秒。根据用户操作和服务器发送信息,我需要在原始V2和V3之间添加多个V1和V2(可以是1对V1-V2,可以是3对V1-V2)。如果我使用的是 ConstraintLayout,当我可以轻松地使用具有垂直方向的 LinearLayout 时,以编程方式添加多个约束是否最好?
现在,就效率、性能和简洁美观的代码而言,与线性布局相比,ConstraintLayout 是否最适合此要求?
android viewgroup android-viewgroup android-constraintlayout
我正在开发一个Android应用程序.在我的一个活动中,我正在使用PullToRefreshListView.有时我得到java.lang.StackOverflowError我的活动异常.我试图解决它,我也搜索谷歌,但没有得到完美的解决方案.
我有两个具有相同功能的活动与PullToRefreshListView具有相同的数据.但我只在一项活动中获得此例外.其他活动工作正常.
这是我的完整StakeTrace.
请帮我.抱歉我的英语不好.
提前谢谢.
java.lang.StackOverflowError
at android.text.TextLine.handleText(TextLine.java:749)
at android.text.TextLine.handleRun(TextLine.java:972)
at android.text.TextLine.measureRun(TextLine.java:416)
at android.text.TextLine.measure(TextLine.java:295)
at android.text.TextLine.metrics(TextLine.java:269)
at android.text.Layout.getLineExtent(Layout.java:942)
at android.text.Layout.draw(Layout.java:405)
at android.text.BoringLayout.draw(BoringLayout.java:400)
at android.widget.TextView.onDraw(TextView.java:5038)
at android.view.View.draw(View.java:10982)
at android.view.View.getDisplayList(View.java:10421)
at android.view.ViewGroup.drawChild(ViewGroup.java:2850)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
at android.view.View.getDisplayList(View.java:10419)
at android.view.ViewGroup.drawChild(ViewGroup.java:2850)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
at android.view.View.getDisplayList(View.java:10419)
at android.view.ViewGroup.drawChild(ViewGroup.java:2850)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
at android.view.View.getDisplayList(View.java:10419)
at android.view.ViewGroup.drawChild(ViewGroup.java:2850)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
at android.view.View.draw(View.java:10985)
at android.view.View.getDisplayList(View.java:10421)
at android.view.ViewGroup.drawChild(ViewGroup.java:2850)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
at android.view.View.getDisplayList(View.java:10419)
at android.view.ViewGroup.drawChild(ViewGroup.java:2850)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
at android.view.View.getDisplayList(View.java:10419)
at android.view.ViewGroup.drawChild(ViewGroup.java:2850)
at android.widget.ListView.drawChild(ListView.java:3223)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
at android.widget.AbsListView.dispatchDraw(AbsListView.java:2092)
at android.widget.ListView.dispatchDraw(ListView.java:3218)
at com.handmark.pulltorefresh.library.PullToRefreshListView$InternalListView.dispatchDraw(PullToRefreshListView.java:293) …Run Code Online (Sandbox Code Playgroud) 如何检测屏幕上的额外手指?例如,我用一根手指触摸屏幕,一段时间后我将第一根手指放在屏幕上,然后用另一根手指触摸屏幕,同时保持第一根手指原样?如何在 Touch Listener 中检测第二个手指触摸?
我的 Android 应用程序中有几个具有相同基本结构的活动,我正在尝试使我的布局变干。重复的代码如下所示。它包含一个带有“返回”和“仪表板”链接的页脚的可滚动区域。还有一个 FrameLayout 用于在可滚动区域的顶部应用渐变。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ScrollView
android:layout_width="match_parent"
android:layout_height="689px">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- THE REAL PAGE CONTENT GOES HERE -->
</LinearLayout>
</ScrollView>
<ImageView
android:src="@drawable/GradientBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="50px"
android:background="?attr/primaryAccentColor">
<Button
android:layout_width="wrap_content"
android:layout_height="26px"
android:layout_gravity="center_vertical"
local:MvxBind="Click GoBackCommand" />
<Button
android:layout_width="wrap_content"
android:layout_height="26px"
local:MvxBind="Click ShowDashboardHomeCommand" />
</FrameLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
为了去重复我的活动,我认为我需要做的是创建一个从 LinearLayout 继承的自定义 ViewGroup。在该代码中,从 XML 文件加载上述内容。我迷路的是如何让活动中的子内容加载到正确的位置。例如,假设我的 Activity 现在包含:
<com.myapp.ScrollableVerticalLayoutWithDashboard
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- THE REAL PAGE CONTENT GOES HERE -->
<TextView android:text"blah blah …Run Code Online (Sandbox Code Playgroud) android android-custom-view viewgroup xamarin android-viewgroup
如何使用自定义形状创建对话框片段?
我有一个图标需要放在对话框窗口的顶部(透明).
对话框本身应该是:
对话框布局:
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_gravity="bottom|center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/server_error_title"
android:layout_marginTop="80dp"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:textAllCaps="true"
android:text="@string/server_error_title"
android:textColor="@color/soft_blue"
android:fontFamily="sans-serif-medium"/>
<View
android:layout_width="60dp"
android:layout_height="2dp"
android:id="@+id/server_error_divider"
android:layout_marginTop="21dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/server_error_title"
android:background="@color/soft_blue" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/server_error_message"
android:layout_marginTop="19dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/server_error_divider"
style="@style/TextDialog"
android:text="@string/server_error_message"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:id="@+id/server_error_large_divider"
android:layout_marginTop="51dp"
android:layout_below="@id/server_error_message"
android:background="@color/greyish"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="@color/white_grey"
android:layout_below="@id/server_error_large_divider">
<Button
android:layout_width="320dp"
android:layout_height="46dp"
android:id="@+id/server_error_close_btn"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
style="@style/TextButton2"
android:text="@string/server_error_close_btn"
android:background="@drawable/big_button_shape" />
</LinearLayout>
</RelativeLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/error_dialog_icon"
android:src="@drawable/ic_error_circle"
android:layout_gravity="center|top"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
但我得到的是这个:
我是否需要自定义ViewGroup,或者可以更简单地完成它?
android android-custom-view android-layout android-dialogfragment android-viewgroup
众所周知,我们可以使用以下命令观察或查询任何应用程序的视图层次结构AccessibilityService:
Create your own accessibility service。
还可以代表用户执行操作:
Developing an Accessibility Service for Android。
我的问题是,我们可以使用修改前台应用程序的视图层次结构吗AccessibilityService?
我已经提到了以下问题:
他们正在做的是使用WindowManager和SYSTEM_ALERT_WINDOW权限在前台的应用程序顶部覆盖视图。这种方法的问题是,如果用户按BACK或HOME,应用程序将被关闭,但视图在屏幕上仍然可见,即使应用程序消失后也是如此。该视图位于视图层次结构的顶部,而不是它的一部分。
AccessibilityNodeInfo对象?View这些与 a或相同吗ViewGroup?AccessibilityService?我的要求是在应用程序本身内显示一个小视图。它必须是应用程序视图层次结构的一部分,以便它与应用程序一起保留或消失。为此,我需要修改应用程序的视图层次结构,即
AccessibilityNodeInfo从服务检索的对象。
我想要的是类似于 的东西addView(),但将 添加View到应用程序本身的视图层次结构中,而不是在其之上。
我们应该怎么做?这可能吗?
更新:
android accessibility android-view android-windowmanager android-viewgroup
为什么当您反复更改包含在父视图中的子视图的可见性并测量父视图时,Android 会返回错误的结果?
我创建了一个简单的测试:一个只有一个 ConstraintLayout 和两个 TextView 的 XML 文件。我会反复将最后一个 TextView 的可见性更改为 GONE 和 VISIBLE。每次我改变 TextView 的可见性时,我都会测量 ConstraintLayout 的宽度和高度。为了改变 TextView 的可见性,我在 ConstraintLayout 上设置了一个点击监听器。
这是上述简单布局的 XML:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/testLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:padding="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/iWillShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="@color/colorOnPrimary"/>
// This one will be hidden on click
<TextView
android:id="@+id/hideMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="World"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/iWillShow"
android:textColor="@color/colorOnPrimary" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
在我的 Kotlin 文件中,我有以下内容:
testLayout.setOnClickListener {
val matchParentMeasureSpec = View.MeasureSpec.makeMeasureSpec((it.parent as View).width, View.MeasureSpec.EXACTLY)
val wrapContentMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED) …Run Code Online (Sandbox Code Playgroud) android android-layout android-view android-viewgroup android-measure