如何在活动视图中使用 xml 设置?

发布于 2024-10-30 05:09:59 字数 2656 浏览 0 评论 0原文

我想在一项活动中显示两个视图。如果我单击第一个视图中的按钮,我想看到第二个视图以及其他视图。 视图的大小不应与屏幕相同,因此我希望将其居中,就像您在first.xml 中看到的那样。

但是,如果我添加视图,则

addContentView(mFirstView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

视图不居中。它们显示在左上角。

我如何使用 xml 设置来将其居中?

first.xml

<?xml version="1.0" encoding="utf-8"?>

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="@drawable/background"
android:layout_gravity="center"
android:minWidth="100dp"
android:minHeight="100dp"
android:paddingBottom="5dp"
>

<LinearLayout android:id="@+id/head" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">           

    <ImageButton android:id="@+id/first_button"
    android:src="@drawable/show_second"
    android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null" />

</LinearLayout>

second.xml 与first.xml 相同,但带有

<ImageButton android:id="@+id/second_button"
     android:src="@drawable/show_first"
    ... />

ShowMe.java

public class ShowMe extends Activity {

View mFirstView = null;
View mSecondView = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        

    initFirstLayout();
    initSecondLayout();
    showFirst();        
}

private void initFirstLayout() {

    LayoutInflater inflater = getLayoutInflater();
    mFirstView = inflater.inflate(R.layout.first, null);

    getWindow().addContentView(mFirstView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    ImageButton firstButton = (ImageButton)mMaxiView.findViewById(R.id.first_button);
    firstButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            ShowMe.this.showSecond();
        }
    });
}

private void initSecondLayout() {
// like initMaxiLayout()
}

private void showFirst() {
    mSecondView.setVisibility(View.INVISIBLE);
    mFirstView.setVisibility(View.VISIBLE);
}

private void showSecond() {
    mFirstView.setVisibility(View.INVISIBLE);
    mSecondView.setVisibility(View.VISIBLE);
}}

希望有人可以提供帮助。 谢谢

I want to show two views in one activity. If I clicked on button in the first view I want to see the second and other way round.
The views should not have the same size as the screen so I want e.g. to center it, like you see in first.xml.

But if I add the views with

addContentView(mFirstView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

the views are not centered. They are shown at top left.

How can I use the xml settings to e.g. center it?

first.xml

<?xml version="1.0" encoding="utf-8"?>

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="@drawable/background"
android:layout_gravity="center"
android:minWidth="100dp"
android:minHeight="100dp"
android:paddingBottom="5dp"
>

<LinearLayout android:id="@+id/head" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">           

    <ImageButton android:id="@+id/first_button"
    android:src="@drawable/show_second"
    android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null" />

</LinearLayout>

second.xml same as first.xml but with

<ImageButton android:id="@+id/second_button"
     android:src="@drawable/show_first"
    ... />

ShowMe.java

public class ShowMe extends Activity {

View mFirstView = null;
View mSecondView = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        

    initFirstLayout();
    initSecondLayout();
    showFirst();        
}

private void initFirstLayout() {

    LayoutInflater inflater = getLayoutInflater();
    mFirstView = inflater.inflate(R.layout.first, null);

    getWindow().addContentView(mFirstView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    ImageButton firstButton = (ImageButton)mMaxiView.findViewById(R.id.first_button);
    firstButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            ShowMe.this.showSecond();
        }
    });
}

private void initSecondLayout() {
// like initMaxiLayout()
}

private void showFirst() {
    mSecondView.setVisibility(View.INVISIBLE);
    mFirstView.setVisibility(View.VISIBLE);
}

private void showSecond() {
    mFirstView.setVisibility(View.INVISIBLE);
    mSecondView.setVisibility(View.VISIBLE);
}}

Hope someone can help.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我也只是我 2024-11-06 05:09:59

为什么不使用 setContentView(R.layout.yourlayout) ?我相信您在 addContentView() 中传递的新 LayoutParams 会覆盖您在 xml 中定义的内容。

此外,ViewGroup.LayoutParams 缺少布局重力设置,因此您必须为要添加视图的布局使用正确的设置(我怀疑它是 FrameLayout) code>,您可以使用 Hierarchy Viewer 检查)。这也是要遵循的一般规则。当使用将布局资源作为参数的方法时,这是自动的(它们可能会要求指定的父级)。

考虑到这一点,您可以使用以下方式设置布局参数:

FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(/* wrap wrap */);
lp.setGravity(Gravity.CENTER);

addContentView(mYourView, lp);

但如果您没有特殊需求,我会推荐 setContentView()

编辑

我的意思是你创建一个布局,如:

~~~/res/layout/main.xml~~~

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="....."
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

然后在你的onCreate()init...Layout()

setContentView(R.layout.main);
FrameLayout mainLayout = (FrameLayout)findViewById(R.id.mainLayout);

// this version of inflate() will automatically attach the view to the
// specified viewgroup.
mFirstView = inflater.inflate(R.layout.first, mainLayout, true);

这将使布局参数保持在 xml 中,因为它知道它需要什么类型。请参阅参考< /a>.

Why don't you use setContentView(R.layout.yourlayout)? I believe the new LayoutParams you're passing in addContentView() are overriding those you defined in xml.

Moreover, ViewGroup.LayoutParams lacks the layout gravity setting, so you would have to use the right one for the layout you're going to add the view to (I suspect it's a FrameLayout, you can check with Hierarchy Viewer). This is also a general rule to follow. When using methods that take layout resources as arguments this is automatic (they might ask for the intended parent).

With this consideration in mind, you could set your layout params with:

FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(/* wrap wrap */);
lp.setGravity(Gravity.CENTER);

addContentView(mYourView, lp);

But I would recommend setContentView() if you have no particular needs.

EDIT

I mean that you create a layout like:

~~~/res/layout/main.xml~~~

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="....."
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

then in your onCreate() or init...Layout():

setContentView(R.layout.main);
FrameLayout mainLayout = (FrameLayout)findViewById(R.id.mainLayout);

// this version of inflate() will automatically attach the view to the
// specified viewgroup.
mFirstView = inflater.inflate(R.layout.first, mainLayout, true);

this will keep the layout params from xml, because it knows what kind it needs. See reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文