所以我只是在Android中玩,发现一些非常奇怪的东西。在我说出错误之前让我给出代码。
这是我的自定义视图组代码:
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);
}
}
}
这是我向CustomView添加(addView()
)的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())
的一个实例并使用布局inflater来扩展我的xml中的视图。然后我使用noteLayout.addView()
将膨胀的视图添加到我的自定义视图组中。我的问题是只显示带有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);
}
}
答案 0 :(得分:1)
好。因此,在我们讨论之后,设法看到发生了什么......我将彻底解释它,因为它可能令人困惑。首先......在 MainActivity 中noteLayout
被初始化像这样:
noteLayout test = new noteLayout(getApplicationContext());
然后与包含2 RelativeLayout
s:
TextView
一起传递
((RelativeLayout)findViewById(R.id.mainActivity))。addView(test);
现在,层次结构看起来像这样:
的RelativeLayout(activity_main)
- RelativeLayout(一个来自xml)
---- TextView(标题)
---- TextView(正文)
在noteLayout
的{{1}}代码中,此行显示为:
RelativeLayout mainParent =(RelativeLayout)getParent();
- 这意味着它获得的mainParent是 MainActivity (onMeasured()
)的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));
}
您可以在此处看到我将第一个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
进行了循环。 另外(我刚刚添加了第二个RelativeLayout的默认度量,您可以根据自己的喜好进行编辑)。这样就可以正确地调用child.getChildCount()
s代码来对代码的期望。
这是运行代码后的屏幕截图。
总的来说。我刚刚编辑了代码的TextView
部分。主要问题在于您所引用的布局和循环。 :d