Android框架布局和动态剪辑
我有一个包含2个其他框架布局的框架布局。我希望其中一个内部FrameLayout永不剪裁,另一个内部FrameLayout(包含位于FrameLayout中心的ImageView)根据用户操作剪裁或不剪裁。我最感兴趣的情况是图像比包含它的FrameLayout大 如果我在包含的布局上设置了setClipChildren(false)或setClipChildren(true),则所有内容都将根据该值进行剪辑或不剪辑 如果在包含的FrameLayout上设置ClipChildren(false),在内部FrameLayout上设置ClipChildren(true),则添加到内部FrameLayout的ImageView不会被剪裁。父布局中的剪辑子布局状态是否会传播到子布局 我在内部框架布局中尝试了水平剪裁、垂直剪裁以及水平填充、垂直填充 我试图避免setClipBounds(),因为大小可能会改变,我希望FrameLayout能够根据Gravity.CENTER和剪辑子项状态确定正确的大小。有没有办法让Android为我做剪辑?否则,我必须计算大小、移动边距并设置剪辑边界。这似乎是安卓应该已经处理的事情 FrameLayout 1不应剪裁。FrameLayout 2应根据应用程序可用的一些其他信息剪裁或不剪裁ImageView 以下是测试应用程序的布局xml和代码:Android框架布局和动态剪辑,android,android-layout,android-framelayout,Android,Android Layout,Android Framelayout,我有一个包含2个其他框架布局的框架布局。我希望其中一个内部FrameLayout永不剪裁,另一个内部FrameLayout(包含位于FrameLayout中心的ImageView)根据用户操作剪裁或不剪裁。我最感兴趣的情况是图像比包含它的FrameLayout大 如果我在包含的布局上设置了setClipChildren(false)或setClipChildren(true),则所有内容都将根据该值进行剪辑或不剪辑 如果在包含的FrameLayout上设置ClipChildren(false),
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/FrameLayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<FrameLayout
android:id="@+id/FrameLayoutClip"
android:layout_width="84dp"
android:layout_height="65dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="12dp"
android:background="@color/backgroundColor"
android:clipChildren="false"
android:clipToPadding="false" >
<CheckBox
android:id="@+id/checkBoxClipMain"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Clip - Main" />
<CheckBox
android:id="@+id/checkBoxClipChild"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:maxLines="1"
android:text="Clip - Child" />
</FrameLayout>
<FrameLayout
android:id="@+id/FrameLayoutText"
android:layout_width="200dp"
android:layout_height="130dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="100dp"
android:background="@color/backgroundColor"
android:clipChildren="false"
android:clipToPadding="false" >
<TextView
android:id="@+id/textView1"
android:layout_width="300dp"
android:layout_height="180dp"
android:text="Large Text to fill up a text view so that we can test how clipping occurs with TextViews and FrameLayouts. It looks like this will be long enough."
android:textAppearance="?android:attr/textAppearanceLarge" />
</FrameLayout>
<FrameLayout
android:id="@+id/FrameLayoutImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="200dp"
android:layout_marginLeft="600dp"
android:clipChildren="false"
android:clipToPadding="false" >
<ImageView
android:id="@+id/imageView"
android:layout_width="200px"
android:layout_height="200px"
android:layout_gravity="center"
android:src="@drawable/test_image" />
</FrameLayout>
发布一张图片,展示你想要实现的目标。我已经编写了一个测试应用程序来探索这个概念。看来Android不会对setClipChildren做我想做的事情。最顶部布局中的setClipChildren将覆盖所有视图组的值。即使在最顶层布局的setClipChildren之后调用子视图组的setClipChildren。我还注意到,WRAP_内容将子视图限制为包含它的父视图组的大小。为了让setClipChildren(false)显示我想要的内容,我必须为孩子的LayoutParams指定一个实际的宽度和高度
public class MainActivity extends Activity implements OnCheckedChangeListener {
private static final String TAG = "MainActivity";
private CheckBox _mainCheckBox = null;
private CheckBox _childCheckBox = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewGroup tempViewGroup = (ViewGroup)findViewById(R.id.FrameLayoutMain);
tempViewGroup.setClipChildren(false);
tempViewGroup = (ViewGroup)findViewById(R.id.FrameLayoutClip);
tempViewGroup.setClipChildren(false);
_mainCheckBox = (CheckBox)findViewById(R.id.checkBoxClipMain);
_mainCheckBox.setOnCheckedChangeListener(this);
_mainCheckBox.setChecked(false);
_childCheckBox = (CheckBox)findViewById(R.id.checkBoxClipChild);
_childCheckBox.setOnCheckedChangeListener(this);
_childCheckBox.setChecked(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1)
{
ViewGroup mainViewGroup = (ViewGroup)findViewById(R.id.FrameLayoutMain);
if (arg0 == _childCheckBox)
{
ViewGroup tempViewGroup = (ViewGroup)findViewById(R.id.FrameLayoutText);
Log.d(TAG, "child onCheckChanged is " + arg1);
tempViewGroup.setClipChildren(arg1);
tempViewGroup = (ViewGroup)findViewById(R.id.FrameLayoutImage);
tempViewGroup.setClipChildren(arg1);
}
else if (arg0 == _mainCheckBox)
{
Log.d(TAG, "main onCheckChanged is " + arg1);
mainViewGroup.setClipChildren(arg1);
}
mainViewGroup.invalidate();
}
}