地方查看内部FrameLayout在安卓
-
27-09-2019 - |
题
我想添加一个图内FrameLayout程,并把它放在一个特定的点内布局有一个特定的宽度和高度。不FrameLayout支持这个吗?如果没有,我应该使用中间ViewGroup来实现这一目标?
int x; // Can be negative?
int y; // Can be negative?
int width;
int height;
View v = new View(context);
// v.setLayoutParams(?); // What do I put here?
frameLayout.addView(v);
我的初步想法是,添加一个AbsoluteLayout的FrameLayout和地方查看内部AbsoluteLayout.不幸的是我刚刚发现,AbsoluteLayout已被否决。
任何指针将不胜感激。谢谢。
解决方案
这是真的,有FrameLayout里所有的孩子都盯住屏幕左侧的顶部,但你仍然有设置其填充一些控制。如果设置不同的填充值,不同的孩子,他们会在不同的FrameLayout的地方显示出来。
其他提示
以下示例(工作代码)显示如何将视图 (EditText) 放置在 FrameLayout 内。它还演示了如何使用 FrameLayout 的 setPadding setter 设置 EditText 的位置(每次用户单击 FrameLayout 时,EditText 的位置都会设置为单击的位置):
public class TextToolTestActivity extends Activity{
FrameLayout frmLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
frmLayout = (FrameLayout)findViewById(R.id.frameLayout1);
frmLayout.setFocusable(true);
EditText et = new EditText(this);
frmLayout.addView(et,100,100);
frmLayout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("TESTING","touch x,y == " + event.getX() + "," + event.getY() );
frmLayout.setPadding(Math.round(event.getX()),Math.round(event.getY()) , 0, 0);
return true;
}
});
}
}
主文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/frameLayout1"
android:layout_height="fill_parent" android:layout_width="fill_parent">
</FrameLayout>
</LinearLayout>
您还可以添加一个余量围绕新添加的视图将其定位在内部的FrameLayout。
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.main); // or some other R.id.xxx
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, metrics.heightPixels - 20, 0, 0);
View v = new View(context);
v.setLayoutParams(params);
frameLayout.addView(v);
这将在的FrameLayout 20像素从屏幕的底部位置。
修改强>完成了例如使得它代表由本身。哦,是的,它确实工作。
从 链接 Quinn1000提供:
你可以增加多儿童FrameLayout,但所有儿童都挂钩的屏幕上。
这意味着你不能把你的视图在一个特定的点内FrameLayout(除非你想让它能在左上角:-)).
如果你需要的绝对的定位看来,试试 AbsoluteLayout:
一个布局可以指定确切的地点(x/y坐标)的儿童。绝对的布局是不够灵活,难以维持比其他类型的布局没有绝对的定位。
作为为设定的宽度和高度来看,还Quinn1000说,你提供的 v.setLayoutParams()
方法一 LayoutParams 目的,根据的容器,你选择了(AbsoluteLayout,LinearLayout,等等。)
这里在计算器上的螺纹在
你怎么setLayoutParams()为ImageView的?
盖它有点。
例如: LinearLayout.LayoutParams的LayoutParams =新LinearLayout.LayoutParams(30,30); yourImageView.setLayoutParams(的LayoutParams);
,表明您需要被限定LinearLayout.LayoutParams(或在您的情况下的FrameLayout.layoutParams)对象传递到您的对象v的setLayoutParams方法。
目前
http://developer.android.com/reference/android/widget /FrameLayout.html
这几乎使得它看起来像你可以问你v到:
generateDefaultLayoutParams()通过这种方法,如果您没有特别定义的参数。
但它晚了,这些链接使我的眼睛流血了一点。让我知道,如果他们任何n帮助: - )