我想知道我的textview是否在可见的屏幕区域内..
我看过以下链接: Android - Get the visible area of a view? Android: how to check if a View inside of ScrollView is visible?
但似乎没有什么对我有用。
我以前检查的代码是
Rect rect = new Rect();
//textview intialized in onCreate as text1
text1.getHitRect(rect);
text1.measure(0, 0);
//layout is the parent layout (linear) in which i am adding the text view
Rect acctualView = new Rect(Math.round(layout.getX()), Math.round(layout.getY()),
Math.round(layout.getX() + layout.getWidth()), Math.round(layout.getY() +layout.getHeight()));
if(Rect.intersects(acctualView, rect))
{
Toast.makeText(context, "visible", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(context, "Not visible ", Toast.LENGTH_LONG).show();
}
textview和layout的每个宽度和高度都给我0“零”,
请让我知道如何解决这个问题并知道视图是否在可见区域内
这是我的完整活动代码
public class MainActivity extends Activity {
private TextView text1, text2, text3, text4, text5, text6;
private Context context;
private LinearLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getLayoutInflater().getContext();
layout = (LinearLayout) findViewById(R.id.parentLayout);
setTextViews();
checkViewAndUpdate();
}
private void checkViewAndUpdate() {
Rect rect = new Rect();
layout.getHitRect(rect);
if (text1.getLocalVisibleRect(rect)) {
Toast.makeText(context, "visible", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Not visible", Toast.LENGTH_LONG).show();
}
}
private void setTextViews() {
text1 = new TextView(context);
text2 = new TextView(context);
text3 = new TextView(context);
text4 = new TextView(context);
text5 = new TextView(context);
text6 = new TextView(context);
text1.setText("text1");
text2.setText("text2");
text3.setText("text3");
text4.setText("text4");
text5.setText("text5");
text6.setText("text6");
text1.setTextSize(30);
text2.setTextSize(30);
text3.setTextSize(30);
text4.setTextSize(30);
text5.setTextSize(30);
text6.setTextSize(30);
//layout.removeAllViews();
layout.addView(text1);
layout.addView(text2);
layout.addView(text3);
layout.addView(text4);
layout.addView(text5);
layout.addView(text6);
}
}
我的布局只包含LinearLayout(空)
答案 0 :(得分:3)
正如我在评论中写的那样,检查onCreate
处的可见矩形是否工作
请在onCreate
结尾处尝试此代码(而不是调用checkAndUpdate() directly
)。
layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
checkViewAndUpdate();
}
});
如果您在onCreate
添加了视图,则视图布局无法完成。是的,这次用户看不到实际的活动,并且没有设置查看的实际尺寸
视图布局结束时会onGlobalLayout
调用(活动可见)
在那个时刻,我们现在可以获得它们的大小和位置了!
仅供参考,您可以致电layout.getViewTreeObserver().removeOnGlobalLayoutListener(this)
onGlobalLayout()
来移除此监听器
答案 1 :(得分:1)
试试这个:
if (yourView.getParent().getVisibility() == View.VISIBLE) {
// visible
} else{
// hiden
}
或者这个:
Rect rect = new Rect();
parentView.getHitRect(rect);
if (imageView.getLocalVisibleRect(rect)) {
// imageView is within the visible window
} else {
// imageView is not within the visible window
}
答案 2 :(得分:0)
为什么不使用for
方法?
for(int i = 0; i < 6; i++){
TextView tv = new TextView(context);
tv.setTextSize(30);
layout.addView(tv);
//Maybe set some listeners...
}