- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我只是在 android 中玩耍,发现了一些非常奇怪的东西。在我说明错误之前,让我给出代码。
这是我的自定义 View 组代码:
package com.ayto.android.cleverpad;
import android.content.Context;
import android.graphics.Color;
import android.graphics.RectF;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
public class noteLayout extends ViewGroup {
float leftOrientationSize = 0;
float rightOrientationSize=0;
public noteLayout(Context activityContext)
{
super(activityContext);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
RelativeLayout mainParent = (RelativeLayout) getParent();
for (int i = 0; i < getChildCount(); i++) {
final View child = getChildAt(i);
child.measure(MeasureSpec.makeMeasureSpec((mainParent.getWidth()/2)-30,MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(200,MeasureSpec.AT_MOST));
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int numberOfChild = getChildCount();
for (int i = 0;i<numberOfChild;i++){
View childView = getChildAt(i);
float childHeight = (float) childView.getMeasuredHeight();
float childWidth = (float) childView.getMeasuredWidth();
RectF rect = new RectF();
rect.bottom = childHeight+20;
rect.top = 20;
rect.left = 20;
rect.right = childWidth+20;
childView.layout((int) rect.left, (int) rect.top, (int) rect.right, (int) rect.bottom);
}
}
}
这是我添加 (addView()
) 到 CustomView 的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:background="@android:color/holo_blue_bright">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Title"
android:id="@+id/displayNoteTitle"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Test"
android:id="@+id/displayNote"
android:padding="5dp"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:ellipsize="end"
android:layout_above="@+id/displayNoteTitle"
android:layout_alignRight="@+id/displayNoteTitle"
android:layout_alignEnd="@+id/displayNoteTitle" />
</RelativeLayout>
所以基本上我所做的是创建我的 noteLayout note = new noteLayout(getApplicationContext())
的实例,并使用布局充气器从我的 xml 中充气 View 。然后我使用 noteLayout.addView()
将膨胀 View 添加到我的自定义 View 组中。我的问题是只显示带有 android:Text = "Title"
的 TextView ,而另一个似乎不呈现。我不太确定为什么会这样。
ActivityMain
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
noteLayout test = new noteLayout(getApplicationContext());
test.addView(getLayoutInflater().inflate(R.layout.Layout,null));
((RelativeLayout) findViewById(R.id.mainActivity)).addView(test);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
最佳答案
好的。所以在我们的讨论之后,设法看到发生了什么..我会彻底解释它,因为它可能会令人困惑..首先..在 MainActivity 中,noteLayout
是像这样初始化:
noteLayout test = new noteLayout(getApplicationContext());
然后与另一个包含 2 个 TextView
的 RelativeLayout
一起传递:
((RelativeLayout) findViewById(R.id.mainActivity)).addView(test);
所以现在,层次结构如下所示:相对布局(activity_main)
-- RelativeLayout(一个来自xml)
---- TextView (标题)
---- TextView (正文)
并且在 onMeasured()
中 noteLayout
的代码中出现了这一行:
RelativeLayout mainParent = (RelativeLayout) getParent();
--也就是说它得到的mainParent是MainActivity的RelativeLayout(R.layout.activity_main
),之后就循环了:
for (int i = 0; i < getChildCount(); i++) {
final View child = getChildAt(i);
child.measure(MeasureSpec.makeMeasureSpec((mainParent.getWidth()/2)-30,MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(200,MeasureSpec.AT_MOST));
}
为了验证这一点,我在循环中放置了一个日志,它只返回 1 个 child 。所以我所做的就是像这样修改循环:
RelativeLayout mainParent = (RelativeLayout) getParent();
final RelativeLayout child = (RelativeLayout) getChildAt(0);
child.measure(MeasureSpec.makeMeasureSpec((mainParent.getWidth() / 2) - 30, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec((mainParent.getHeight() / 2) - 30, MeasureSpec.EXACTLY));
Log.d("SAMPLE", "RelativeLayout Child childcount: " + child.getChildCount());
for (int i = 0; i < child.getChildCount(); i++) {
Log.d("SAMPLE", "In loop: " + i);
final View childOfChild = child.getChildAt(i);
childOfChild.measure(MeasureSpec.makeMeasureSpec((mainParent.getWidth() / 2) - 30, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(200, MeasureSpec.AT_MOST));
}
您可以在这里看到,我将第一个 RelativeLayout
的子级视为另一个 RelativeLayout
然后使用 child.getChildCount()
进行循环>。 此外(我刚刚为第二个 RelativeLayout 添加了默认度量,您可以将其编辑为您喜欢的任何一个)。这会根据您对代码的期望正确调用 TextView
。
这是运行代码后的屏幕截图。
总的来说。我刚刚编辑了您代码的 onMeasure()
部分。主要问题在于您引用和循环的布局。 :D
关于java - 即使调用 onMeasure(),Textview 也不会显示在自定义 View 组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36000863/
我做了一个自定义 View 。如果我将 View 添加到布局 XML 文件并将高度设置为 fill_parent“specSize”返回 0。为什么? 代码: @Override protec
我目前正在开发一个相机应用程序,我必须根据(全屏)应用程序 View 的大小设置相机预览大小。该过程涉及等待 onMeasure 事件获取 View 的大小,并根据其他几个参数(填充、相机图片大小等)
我在 recyclerView 中使用了相当复杂的 recyclerView 项目布局。我已经开始有一段时间的性能问题了。每次我运行应用程序时,我都会在为 recyclerView 中的 View 扩
有没有办法在它的 onmeasure 中更改我的自定义 View 的 subview (或 onlayout,ondraw,我实际上不知道把它放在哪里:) 我有这段代码,它应该缩小我从 LinearL
我正在尝试根据可用空间的高度和宽度来缩放自定义 View 。所以它应该填充绿色箭头高度和宽度的空间。但是我从 heightMeasureSpec = View.MeasureSpec.getSize(
希望得到帮助。我的函数调用 onMeasure 的问题。在我的 xml 文件中(RelativeLayput)- 3 个元素:TextView、RelativeLayout 并将它们包含在我的类中。
我有一个非常简单的自定义 View ,覆盖了 onMeasure()。我的onMeasure() 根据背景Drawable 决定大小。首次从 xml 创建 View 时,一切正常。但是后来我以编程方式
我制作了一个覆盖了 onDraw() 的自定义 View ,它在 Canvas 上绘制位图。当我在布局文件中指定我想要 wrap_content 时,它仍然会填满整个屏幕。 onMeasure() 是
所以我尝试将我的一些回收器 ViewHolders 重构为 ConstraintLayouts。在我这样做之后,我看到的东西让我感到震惊。展开单个 View 比通常的 LinearLayout 多花费
我需要覆盖 onMeasure 方法,但在尝试覆盖时出现错误:DrawLnClass.DrawLn 类型的方法 OnMeasure(int, int) 必须覆盖父类(super class)方法 这是
我正在尝试修复阻止我的 Android 应用程序运行的异常。我用谷歌搜索了这个异常,发现其他人也有同样的问题。有些人通过为他们的 RecycledView 包含一个 LinearLayoutManag
什么时候 View.onMeasure(int widthMeasureSpec, int heightMeasureSpec) 打电话了?我有一个 Activity需要在 onMeasure 之后执
我有一项 Activity 和一种 View 。此 View 的目的是显示方形网格。正方形边长等于屏幕宽度除以水平存在的正方形数量。 @Override protected void onCreate
我编写了自己的布局(子类化 RelativeLayout)。我从调试器中看到 onMeasure() 和 onLayout() 被连续调用。这是正常行为吗?堆栈跟踪从 dispatchMessage(
在我当前的应用程序中,我有很多小 View 相互叠加,以至于方法分析表明测量等占了设置后应用程序一般使用的大部分 cpu 时间。 鉴于这些 View 都没有改变其父 View 中的大小或位置,是否可以
我正在做一个非常简单的功能,比如在 TextView 的末尾添加省略号。 我可以在 onMeasure()、onLayout() 和 OnGlobalLayoutListener() 调用中添加该功能
我有一个 Activity ,其中有几个线性布局的 View ,一个是 TextView ,一个是我的游戏的主视图,这是我在代码中使用的自定义 View 。 当我将以下代码添加到我的 Activity
我有一个 SurfaceView。我只是从其他线程手动绘制它。 运行良好。但是平均运行 10-20 分钟后,我得到了这个异常: 01-14 08:51:25.000 3740-3740/com.
有一个特定大小的 TextView,每当设置给它的文本太长时,我想将它重新设置为某个预设值。 为了实现这一点,我重写了 onMeasure 并调用了 setText。但是,这不会影响 TextView
编辑:当我对此进行调试并尝试了各种更改时,我意识到我发布的原始代码并不真正相关,所以我删除了它: 我有一个 Activity可以在 3 个不同的 Fragment 之间切换s 当用户按下 3 个 Ic
我是一名优秀的程序员,十分优秀!