(Android)如何在我的xml文件中找到所有ImageView?

时间:2015-08-22 17:27:19

标签: xml android-imageview

我想将每个图像视图添加到数组列表中,无论UI中有多少(因此可以在不更改代码的情况下进行更改)。有没有办法做到这一点?像findAllViews(ImageView)方法这样的东西是完美的。

3 个答案:

答案 0 :(得分:3)

您可以使用以下方法迭代xml中的所有视图:

RelativeLayout yourLayout = (RelativeLayout)findViewById(R.id.layout);

 for (int i = 0; i < yourLayout .getChildCount(); i++) {

        View subView = yourLayout .getChildAt(i);

        if (subView instanceof ImageView) {
            ImageView imageView = (ImageView) subView;
          //manipulate the imageView
        }
    }

答案 1 :(得分:0)

您可以这样做:

int childsNumber = relativeLayout.getChildCount() - 3;

其中relativeLayout是仅包含所有ImageView的布局。如果您对relativeLayout有其他视图,则可以计算其他视图并减去它们。这是一个例子:

如果您有多个带有2个TextView和1个按钮的ImageView,您将执行以下操作:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GamePanel extends JPanel implements ActionListener {
    private static final int PREF_W = 800;
    private static final int PREF_H = 600;
    Player player = new Player();
    Ball ball = new Ball();

    public GamePanel() {
        Timer time = new Timer(50, this);
        time.start();

        // !! set key bindings
        int condition = WHEN_IN_FOCUSED_WINDOW;
        InputMap inputMap = getInputMap(condition);
        ActionMap actionMap = getActionMap();

        KeyStroke up = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
        inputMap.put(up, up.toString());
        actionMap.put(up.toString(), new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                player.setyv(-5);
            }
        });

        KeyStroke down = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
        inputMap.put(down, down.toString());
        actionMap.put(down.toString(), new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                player.setyv(5);
            }
        });
    }

    private void update() {
        player.update();
        ball.update();
    }

    // !! public void paintComponent(Graphics g) {
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); // !!
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, PREF_W, PREF_H); // !!
        player.paint(g);
        ball.paint(g);
    }

    // !!
    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    public void actionPerformed(ActionEvent e) {
        update();
        repaint();
    }

    // !!
    private static void createAndShowGui() {
        GamePanel mainPanel = new GamePanel();

        JFrame frame = new JFrame("GamePanel");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

interface Playable {
    void update();

    void paint(Graphics g);
}

class Player implements Playable {
    private static final Color PLAYER_COLOR = Color.RED;
    private static final Font FONT = new Font(Font.SANS_SERIF, Font.BOLD, 24);
    private int x = 400;
    private int y = 400;
    private int yv = 0;
    private int xv = 0;

    @Override
    public void update() {
        y += yv;
        x += xv;
    }

    public void setyv(int i) {
        yv += i;
    }

    @Override
    public void paint(Graphics g) {
        g.setFont(FONT);
        g.setColor(PLAYER_COLOR);
        g.drawString("P", x, y);
    }

}

class Ball implements Playable {

    @Override
    public void update() {
        // TODO Auto-generated method stub

    }

    @Override
    public void paint(Graphics g) {
        // TODO Auto-generated method stub

    }

}

其中3是不是ImageView的总视图。

当然你可以使用任何其他类型的布局而不是RelativeLayout,只需遵循我写的逻辑。希望有所帮助。

答案 2 :(得分:0)

如果您正在考虑 Root

下的所有ImageView
 /**
 * View is the root view
 * i.e in case of activity just pass R.id.content
 * or in case of Fragment pass it the reference you get from LayoutInflator
 */

 List<View> listOfImgViews = new ArrayList<>();

 private void viewIterator(View view){

            if (view instanceof ViewGroup && !(view instanceof AdapterView)){

                for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++){

                    if(((ViewGroup) view).getChildAt(i) instanceof ImageView){
                        listOfImgViews.add(((ViewGroup) view).getChildAt(i));

                    }else if( ((ViewGroup) view).getChildAt(i) instanceof ViewGroup && !(((ViewGroup) view).getChildAt(i) instanceof AdapterView)){
                        viewIterator(((ViewGroup) view).getChildAt(i));
                    }


                }

            }
        }