gpt4 book ai didi

java - 即使调用 onMeasure(),Textview 也不会显示在自定义 View 组中

转载 作者:行者123 更新时间:2023-11-30 01:29:10 25 4
gpt4 key购买 nike

所以我只是在 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 ,而另一个似乎不呈现。我不太确定为什么会这样。

enter image description here

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 个 TextViewRelativeLayout 一起传递:

((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

这是运行代码后的屏幕截图。

enter image description here

总的来说。我刚刚编辑了您代码的 onMeasure() 部分。主要问题在于您引用和循环的布局。 :D

关于java - 即使调用 onMeasure(),Textview 也不会显示在自定义 View 组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36000863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com