我有一个 android 布局设计,其中我有一个 AppBar
布局,它将包含一个 Toolbar
和一个带有圆圈的 LinearLayout
设计 Drawable
作为 TextView
。这是相同的高度。因此,当我尝试获取 Toolbar
或 AppBar
高度/宽度时,它只返回 0。
activity_main.xml:
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@color/colorPrimary"
app:layout_scrollFlags="enterAlways" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary"
android:orientation="horizontal"
app:layout_scrollFlags="enterAlways">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/marked_questions"
style="@style/textview_summary_omr_toolbar_count"
android:background="@drawable/bg_percentage_default"
android:text="18" />
<TextView
style="@style/textview_heading_summary_omr_toolbar"
android:text="Marked" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/correct"
style="@style/textview_summary_omr_toolbar_count"
android:background="@drawable/bg_percentage_6"
android:text="18"
/>
<TextView
style="@style/textview_heading_summary_omr_toolbar"
android:text="Correct" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/wrong"
style="@style/textview_summary_omr_toolbar_count"
android:background="@drawable/bg_percentage_wrong"
android:text="18" />
<TextView
style="@style/textview_heading_summary_omr_toolbar"
android:text="Wrong" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/conflicts"
style="@style/textview_summary_omr_toolbar_count"
android:background="@drawable/bg_percentage_3"
android:text="0" />
<TextView
style="@style/textview_heading_summary_omr_toolbar"
android:text="Conflicts" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
style="@style/textview_summary_omr_toolbar_count"
android:text="Score:"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/marks_scored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="18/30"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
MainActivity.java:
在这里,我正在初始化 Toolbar
和 AppBar
并尝试在日志中打印两者的宽度和高度,结果为零。
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private AppBarLayout app_bar;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
app_bar = (AppBarLayout) findViewById(R.id.app_bar);
toolbar = (Toolbar) findViewById(R.id.toolbar);
logToolbarLayoutParams();
}
private void logToolbarLayoutParams() {
Log.d(TAG,"Toolbar width/Height"+this.toolbar.getWidth()+"/"+this.toolbar.getHeight());
Log.d(TAG,"App_bar width/height"+this.app_bar.getWidth()+"/"+this.app_bar.getHeight());
}
我在 stackoverflow 中也提到过这个问题。 .
final AppBarLayout app_bar = (AppBarLayout) findViewById(R.id.app_bar);
ViewTreeObserver vto = app_bar.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
app_bar.getViewTreeObserver().removeGlobalOnLayoutListener(this);
Log.d(TAG, "Global layout");
logToolbarLayoutParams();
}
});
如果我在我的 MainActivity
中添加以下代码,它会为我提供正确的 Toolbar
高度和宽度。从我之前提到的stackoverflow问题,我知道, View 没有绘制在屏幕上,所以我必须等待。但我的疑问是,我指定的设计不是一个复杂的设计吗?
为什么在屏幕上画画要花很多时间?我错过了什么吗?
我已经使用 Hierarchy Viewer 来找出导致子布局加载缓慢的原因。我发现我正在使用多个 LinearLayout
,其中单个 RelativeLayout
可用于获得相同的结构。我改变了我的布局设计如下。
我仍然面临同样的问题。
更新:activity_main.xml:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@color/colorPrimary"
app:layout_scrollFlags="enterAlways" />
<RelativeLayout
android:id="@+id/summary_bottom_sheet"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/marked_questions"
style="@style/textview_summary_omr_toolbar_count"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@drawable/bg_percentage_default"
android:text="18" />
<TextView
android:id="@+id/marked_textview"
style="@style/textview_heading_summary_omr_toolbar"
android:layout_toEndOf="@+id/marked_questions"
android:layout_toRightOf="@+id/marked_questions"
android:text="Marked" />
<TextView
android:id="@+id/correct"
style="@style/textview_summary_omr_toolbar_count"
android:layout_toEndOf="@+id/marked_textview"
android:layout_toRightOf="@+id/marked_textview"
android:background="@drawable/bg_percentage_6"
android:text="18"
/>
<TextView
android:id="@+id/correct_textview"
style="@style/textview_heading_summary_omr_toolbar"
android:layout_toEndOf="@+id/correct"
android:layout_toRightOf="@+id/correct"
android:text="Correct" />
<TextView
android:id="@+id/wrong"
style="@style/textview_summary_omr_toolbar_count"
android:layout_toEndOf="@+id/correct_textview"
android:layout_toRightOf="@+id/correct_textview"
android:background="@drawable/bg_percentage_wrong"
android:text="18" />
<TextView
android:id="@+id/wrong_textview"
style="@style/textview_heading_summary_omr_toolbar"
android:layout_toEndOf="@+id/wrong"
android:layout_toRightOf="@+id/wrong"
android:text="Wrong" />
<TextView
android:id="@+id/conflicts"
style="@style/textview_summary_omr_toolbar_count"
android:layout_toEndOf="@+id/wrong_textview"
android:layout_toRightOf="@+id/wrong_textview"
android:background="@drawable/bg_percentage_3"
android:text="0" />
<TextView
android:id="@+id/conflicts_textview"
style="@style/textview_heading_summary_omr_toolbar"
android:layout_toEndOf="@+id/conflicts"
android:layout_toRightOf="@+id/conflicts"
android:text="Conflicts" />
<TextView
android:id="@+id/score_textview"
style="@style/textview_summary_omr_toolbar_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/conflicts_textview"
android:layout_toRightOf="@+id/conflicts_textview"
android:background="@null"
android:gravity="center"
android:text="Score:"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/marks_scored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_toEndOf="@+id/score_textview"
android:layout_toRightOf="@+id/score_textview"
android:gravity="center"
android:text="18/30"
android:textColor="@android:color/white"
android:textSize="18sp" />
</RelativeLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
最佳答案
你想错了。您的布局没有任何问题(因为它与您的问题有关)。为了证明这一点,完全改变你的布局并重试
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
Log.d("SomeTag", "w: " + recyclerView.getWidth() + ", h: " + recyclerView.getHeight());
Log.d("SomeTag", "mw: " + recyclerView.getMeasuredWidth() + ", mh: " + recyclerView.getMeasuredHeight());
}
只有 1 个 child 的 View 的输出仍然是
12-14 10:17:04.902 3004-3004/? D/SomeTag: w: 0, h: 0
12-14 10:17:04.902 3004-3004/? D/SomeTag: mw: 0, mh: 0
所以您的布局没有任何问题(除非您用词不当。您调用方法 logToolbarLayoutParams,但您根本没有访问布局参数。您试图在布局之前获取 View 对象的属性pass 发生了。也许这是你的困惑)。因此,您缺少的是对 Android 布局系统、 View 组和 View 的理解。 Romain Guy 曾在 Devoxx 上就如何构建自定义 FlowLayout 进行了一场非常酷的演讲。网站已关闭或视频已被删除但here's the code in GitHub如果查看 onLayout 方法,您可以看到 ViewGroup 循环遍历它的所有子项并调用 child.layout()。在调用此方法之前,view.getWidth 和 view.getHeight 方法将返回 0。操作系统会在调度时调用此方法。所以基本上,您甚至在操作系统安排布局之前就试图立即访问宽度和高度。自己制作一个自定义 View 组,添加一些断点,然后试一试。
关于android - Child Views 花太多时间在屏幕上画画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40988916/