我有多个线性布局,如何一次更改部分或全部背景,然后再次更改它们,等等?

发布于 2024-11-16 05:36:36 字数 1371 浏览 2 评论 0原文

在我的活动中,我有大约 40 个线性布局......基本上是 5 个父母和所述父母的其余孩子。

我想要做的是改变一些人的背景,然后单击一个按钮并改变其他人的背景。

我遇到的问题...不幸的是,我现在没有确切的代码,一旦我更改背景,我必须将其设置为空。

我的 java 类中的代码看起来像这样...

mlayout1= findViewById(R.id.layout1);

mlayout2= findViewById(R.id.layout2);

mlayout3= findViewById(R.id.layout3);

mlayout4= findViewById(R.id.layout3);

if variable = 1{

mlayout1.setBackgroundResource(R.drawable.background_img);
mlayout2.setBackgroundResource(R.drawable.background_img);
mlayout3.setBackgroundResource(R.drawable.background_img);
mlayout4.setBackgroundResource(R.drawable.background_img);
}

if variable = 2{

mlayout1.setBackgroundResource(R.drawable.background_img);

mlayout2.setBackgroundResource(null);

mlayout3.setBackgroundResource(R.drawable.background_img);

mlayout4.setBackgroundResource(null);
}

if variable = 3{

mlayout1.setBackgroundResource(R.drawable.background_img);

mlayout2.setBackgroundResource(null);

mlayout3.setBackgroundResource(null);

mlayout4.setBackgroundResource(null);
}

等等...

一个问题是我需要将背景更改为大约 30 种布局以及我正在使用的大约 500 种不同组合。 ..

我遇到的最大问题是在应用新的背景组合之前必须将所有背景重新设置为空...

我有一个包含所有不同组合的表格。

我希望能够通过点击下一个和/或上一个按钮或输入组合 ID 号来滚动浏览它们,并使背景的正确组合亮起,然后在点亮下一个组合之前将它们全部重置...... ...

也许我需要另一个活动来将所有内容重置为空,然后再绘制新的背景组合或我不确定如何最好地有效地做到这一点。

我是 Android 新手,我很头疼,因为我几周来一直在试图解决这个问题......

In my activity I have about 40 linear layouts... Basically 5 parents and the rest children of said parents.

What I want to be able to do is to change the background of some, then click a button and change the background of others.

The issue I'm having... And unfortunately I don't have my exact code with me right now is that once I change a background, I have to set it to null.

The code that I have in my java class looks something like this...

mlayout1= findViewById(R.id.layout1);

mlayout2= findViewById(R.id.layout2);

mlayout3= findViewById(R.id.layout3);

mlayout4= findViewById(R.id.layout3);

if variable = 1{

mlayout1.setBackgroundResource(R.drawable.background_img);
mlayout2.setBackgroundResource(R.drawable.background_img);
mlayout3.setBackgroundResource(R.drawable.background_img);
mlayout4.setBackgroundResource(R.drawable.background_img);
}

if variable = 2{

mlayout1.setBackgroundResource(R.drawable.background_img);

mlayout2.setBackgroundResource(null);

mlayout3.setBackgroundResource(R.drawable.background_img);

mlayout4.setBackgroundResource(null);
}

if variable = 3{

mlayout1.setBackgroundResource(R.drawable.background_img);

mlayout2.setBackgroundResource(null);

mlayout3.setBackgroundResource(null);

mlayout4.setBackgroundResource(null);
}

and etc...

One problem is that there are about 30 layouts that I need to change the background to and about 500 different combinations that I'm working with...

The biggest problem that I'm having is having to re-set all the backgrounds to null before applying the new combination of backgrounds...

I have a table with all the different combinations in it.

I would like to be able to scroll through them by hitting a next and/or previous button or typing in a combination ID number and have the correct combination of backgrounds light up, then have them all reset before lighting up the next combination......

Maybe I need another activity to reset everything to null before I draw the new combination of backgrounds or something I'm not sure how best to do this efficiently.

I'm new to Android and my head hurts as I've been trying to figure this out for weeks now...

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

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

发布评论

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

评论(1

一袭白衣梦中忆 2024-11-23 05:36:36

您可能想做的第一件事是将布局视图放入数组或列表或其他内容中,这样您就不必在每个视图的 ID 名称中进行编码:

public void onCreate(Bundle b) {
    setContentView(R.layout.main);

    List<View> myViews = new ArrayList<View>();
    myViews.add(findViewById(R.id.layout1));
    myViews.add(findViewById(R.id.layout2));
    // etc.
}

然后您可以在数组中以编程方式直接访问它们。但这仍然是很多代码。

如果您有权访问其父级,则可以在做出选择时迭代其子级:

public void performSelection(int selection) {
    // One of your parents that holds a lot of child views
    ViewGroup parent = (ViewGroup)findViewById(R.id.parent_1);

    for(int i = 0; i < parent.getChildCount(); i++) {
        View v = parent.getChildAt(i);

        // If selection is 1, this sets the drawable background on every item.
        // If 2, then sets drawable on 0, 2, 4, 6, ... and null on 1, 3, 5, ...
        // If 3, then sets drawable on 0, 3, 6, 9, ... and null on 1, 2, 4, 5, ...
        if (i % selection == 0) {
            v.setBackgroundResource(R.drawable.background_img);
        } else {
            v.setBackgroundResource(null);
        }
    }
}

不过要小心,这将查看所请求父级的每个子级。确保它只包含您想要更改背景的视图。

另一种方法:如果您的布局视图在 XML 中基本相同,但具有不同的 ID,您可能会考虑为其创建一个单独的 XML 资源,然后在创建活动时将它们动态加载到您的父级或数组中。在这种情况下,您的主布局 XML 文件将具有parent_1 元素,但没有子视图,因为它们会在运行时添加。

layout/one_element.xml

<LinearLayout ...>
     <TextView android:id="@+id/element_text" ... />
     <!-- etc -->
</LinearLayout>

那么你可以做这样的事情:

public void onCreate(Bundle b) {
    setContentView(R.layout.main);

    // One of your parents that will hold a lot of child views
    ViewGroup parent = (ViewGroup)findViewById(R.id.parent_1);

    for (int i = 0; i < NUM_CHILDREN; i++) {
        View newView = getLayoutInflater().inflate(R.layout.one_element,
                                                   null);
        // Set anything specific to the new view
        // eg.
        TextView tv = (TextView)newView.findViewById(R.id.element_text);
        tv.setText(String.format("Hi, I'm element %d!", i));

        // Add it to the parent
        parent.addView(newView);
    }
}

然后你可以使用上面的performSelection方法根据需要更改背景。

The first thing you might want to do is put your layout views into an array or list or something, so you don't have to code in the ID name of every single one:

public void onCreate(Bundle b) {
    setContentView(R.layout.main);

    List<View> myViews = new ArrayList<View>();
    myViews.add(findViewById(R.id.layout1));
    myViews.add(findViewById(R.id.layout2));
    // etc.
}

Then you can access them directly and programmatically in the array. But that's still a lot of code.

If you have access to their parent you could iterate over its children when making your choice:

public void performSelection(int selection) {
    // One of your parents that holds a lot of child views
    ViewGroup parent = (ViewGroup)findViewById(R.id.parent_1);

    for(int i = 0; i < parent.getChildCount(); i++) {
        View v = parent.getChildAt(i);

        // If selection is 1, this sets the drawable background on every item.
        // If 2, then sets drawable on 0, 2, 4, 6, ... and null on 1, 3, 5, ...
        // If 3, then sets drawable on 0, 3, 6, 9, ... and null on 1, 2, 4, 5, ...
        if (i % selection == 0) {
            v.setBackgroundResource(R.drawable.background_img);
        } else {
            v.setBackgroundResource(null);
        }
    }
}

Careful though, that will look at EVERY child for the requested parent. Make sure it only contains the views that you want to change the backgrounds of.

Another approach: If your layout views are all basically the same in XML but with different IDs, you might consider making a separate XML resource for one and then loading them dynamically at activity creation into your parents or an array. In this case, your main layout XML file would have the parent_1 element but with no child views, as they'd be added at runtime.

layout/one_element.xml:

<LinearLayout ...>
     <TextView android:id="@+id/element_text" ... />
     <!-- etc -->
</LinearLayout>

Then you can do something like this:

public void onCreate(Bundle b) {
    setContentView(R.layout.main);

    // One of your parents that will hold a lot of child views
    ViewGroup parent = (ViewGroup)findViewById(R.id.parent_1);

    for (int i = 0; i < NUM_CHILDREN; i++) {
        View newView = getLayoutInflater().inflate(R.layout.one_element,
                                                   null);
        // Set anything specific to the new view
        // eg.
        TextView tv = (TextView)newView.findViewById(R.id.element_text);
        tv.setText(String.format("Hi, I'm element %d!", i));

        // Add it to the parent
        parent.addView(newView);
    }
}

Then you can use the performSelection method above to change the backgrounds as needed.

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