在Android中将视图放置在FrameLayout中

11

我想在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已被弃用。

非常感谢任何提示。


你已经解决了那个问题吗?我对非AbsoluteLayout的解决方案很感兴趣 :) - WarrenFaith
5个回答

12

以下示例(可运行的代码)展示了如何将一个视图(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;
        }
    });

}

main.xml

<?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>

这与问题有什么关系? - zeh
2
好的...这不是一个Framelayout中的视图吗?此外,我正在展示如何使用setPadding设置器将嵌入式视图定位到特定位置。 - tulio84z

3
您可以在新添加的视图周围添加边距,以将其定位在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像素。

编辑:完善了示例,使其独立存在。是的,它确实有效。


  1. 什么是宽度?它来自哪里?
  2. metric.width不存在,应该使用metric.widthPixels。
  3. 这样做不起作用,即使它能够工作,也不会因为你没有添加v。
- glace

2

虽然使用FrameLayout时所有子元素都将固定在屏幕的左上角,但您仍然可以通过设置它们的填充来进行一些控制。如果您为不同的子元素设置不同的填充值,则它们将显示在FrameLayout中的不同位置。


9
问题在于点击监听器将覆盖整个区域,从左上角开始... - Pepster

1

从Quinn1000提供的链接中:

您可以将多个子项添加到FrameLayout中,但所有子项都固定在屏幕的左上角。

这意味着您无法将您的视图放置在FrameLayout内的特定位置(除非您希望它位于左上角 :-))。

如果您需要视图的绝对定位,请尝试使用AbsoluteLayout

一种布局,允许您指定其子项的确切位置(x/y坐标)。绝对布局比其他类型的布局更不灵活且更难以维护,因为它需要绝对定位。

至于设置视图的宽度和高度,就像Quinn1000所说的那样,您可以根据所选容器(AbsoluteLayout、LinearLayout等)提供v.setLayoutParams()方法的LayoutParams对象。


2
很遗憾,AbsoluteLayout已经被弃用。 - hpique
你说得对。我在之前查看 API 时错过了那个。我想,你可以通过调整 FrameLayout 中 View 的 margin-left/top 属性来获得类似 AbsoluteLayout 的体验。 - Matthias Schippling
我如何以编程方式实现这个? - hpique
请查看FrameLayout.LayoutParams的API(链接在我的答案中):它扩展了MarginLayoutParams,提供了一个“setMargins”方法。 - Matthias Schippling

0

在 stackOverflow 上的这个帖子

如何为 ImageView 设置 setLayoutParams()?

有所涉及。

例如: LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30); yourImageView.setLayoutParams(layoutParams);

意味着您需要定义一个 LinearLayout.LayoutParams(或在您的情况下是 FrameLayout.layoutParams)对象,以传递给 v 对象的 setLayoutParams 方法。

在此处

http://developer.android.com/reference/android/widget/FrameLayout.html

这几乎让人觉得你可以通过此方法要求您的v:

如果您没有明确定义参数,则通过此方法生成默认布局参数。

但现在已经很晚了,这些链接让我的眼睛有点流血。如果它们有帮助,请告诉我 :-)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接