这仅供参考,我认为它不实用,未经测试,......
既然答案被接受了,我将陈述这段代码的问题(我现在能想到的):
- 正如@user2558882 在他对答案的评论中所说,如果子类使用
setContentView(view, params)
这些参数就会丢失。
- 这不适用于膨胀的视图——即适配器;但是,您可以通过调用
fix(View)
任何膨胀视图来解决。
- 它只适用于颜色。这也可以修复。
- 我认为这可能适用于sherlock-actionbar。
- 代码未测试,请测试。
- 它通过添加一个RelativeLayout 将你的整个视图树再移一层。这可以使用
addContentView
. 我确实使用了这个 RelativeLayout,因为我回答得很快,而且我还认为当前代码在所有手机和 api 上都能更好地工作。这是真的,因为整个解决方案都压缩在一个行为非常一致的 RelativeLayout 中。毕竟我只使用RelativeLayout.LayoutParams
了这个 ViewGroup 最基本的功能。
解决方案
创建一个BaseActivity.java:
package mobi.sherif.overlay;
import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
public class BaseActivity extends Activity {
RelativeLayout mRl;
View mImage;
View mContent;
@Override
public void setContentView(int layoutResID) {
setContentView(LayoutInflater.from(this).inflate(layoutResID, null));
}
@Override
public void setContentView(View view) {
setContentView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
@Override
public void setContentView(View view, LayoutParams params) {
mContent = view;
fix(view);
push(view);
}
protected void refix() {
fix(mContent);
}
private void push(View view) {
mRl = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
mRl.addView(view, params);
mImage = new View(this);
mImage.setVisibility(View.GONE);
params = new RelativeLayout.LayoutParams(0, 0);
mRl.addView(mImage, params);
super.setContentView(mRl);
}
protected void fix(View child) {
if (child == null)
return;
doFix(child);
if (child instanceof ViewGroup) {
fix((ViewGroup) child);
}
}
private void fix(ViewGroup parent) {
for (int i = 0; i < parent.getChildCount(); i++) {
fix(parent.getChildAt(i));
}
}
private void doFix(View child) {
if(child.getTag()!=null && child.getTag().getClass() == String.class) {
String color = (String) child.getTag();
int theColor;
try {
theColor = Color.parseColor(color);
child.setTag(theColor);
} catch (Exception e) {
theColor = -1;
}
if(theColor != -1) {
child.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mImage.setBackgroundColor((Integer) v.getTag());
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mImage.getLayoutParams();
params.leftMargin = getLeftWithRespectTo(v, mRl);
params.topMargin = getTopWithRespectTo(v, mRl);
params.width = v.getWidth();
params.height = v.getHeight();
mImage.setVisibility(View.VISIBLE);
mImage.requestLayout();
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
mImage.setVisibility(View.GONE);
default:
break;
}
return v.onTouchEvent(event);
}
});
}
}
}
private int getLeftWithRespectTo(View view, View relative) {
int left = 0;
View temp = view;
do {
left += temp.getLeft();
temp = (View) temp.getParent();
}
while(temp!=relative);
return left;
}
private int getTopWithRespectTo(View view, View relative) {
int top = 0;
View temp = view;
do {
top += temp.getTop();
temp = (View) temp.getParent();
}
while(temp!=relative);
return top;
}
}
现在扩展这个 BaseActivity 和你想要覆盖它的任何视图,使用android:tag="#AARRGGBB"
如下颜色:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="#331111ff"
android:text="@string/hello_world" />
测试:如果你像这样在activity_main.xml中使用xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="17dp"
android:layout_marginTop="90dp"
android:tag="#331111ff"
android:text="TextView" />
</RelativeLayout>
并将其用作您的MainActivity.java:
package mobi.sherif.overlay;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
findViewById(R.id.textView1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "text tpicjed", Toast.LENGTH_SHORT).show();
}
});
}
}