【问题标题】:Android centre an image programmaticallyAndroid以编程方式居中图像
【发布时间】:2014-01-11 10:54:18
【问题描述】:

好的,我正在尝试回收这些帖子中给出的关于以编程方式居中图像但没有成功的建议。我的应用程序基本上可以正常工作,它由一个带有图像的微调器和它下面的一些 TextViews 组成。这些内容取决于微调器的位置。我想要的是当微调器处于其默认位置时图像居中对齐,并在选择某些内容时左对齐。布局在布局文件中定义,一个用于纵向,另一个用于横向,这是我正在努力解决的代码部分:-

@Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {

            // The variable index identifies the position the spinner is in.
            // TextView name, country and description... locks to the
            // TextViews defined in the activity_main.xml layout files.
            int index = parent.getSelectedItemPosition();
            TextView name = (TextView) findViewById(R.id.name);
            TextView country = (TextView) findViewById(R.id.country);
            TextView description = (TextView) findViewById(R.id.description);

            // Now we'll check to see if we're in the None Selected spinner
            // position. If true we'll dump the name, country and
            // description TextViews otherwise these will be shown.

            if (index == 0) {

                image.setImageResource(imgs.getResourceId(
                        spinner1.getSelectedItemPosition(), -1));
                // Try and centre the image when none is selected
                RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) image
                        .getLayoutParams();
                lp.addRule(RelativeLayout.CENTER_VERTICAL);
                image.setLayoutParams(lp);

                name.setVisibility(View.GONE);
                country.setVisibility(View.GONE);
                description.setVisibility(View.GONE);

            } else {

                image.setImageResource(imgs.getResourceId(
                        spinner1.getSelectedItemPosition(), -1));
                // Try and left align the image when the spinner is NOT in
                // the None Selected position.
                RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) image
                        .getLayoutParams();
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                image.setLayoutParams(lp);

                name.setVisibility(View.VISIBLE);
                country.setVisibility(View.VISIBLE);
                description.setVisibility(View.VISIBLE);

                name.setText(leaders[index]);
                country.setText(states[index]);
                description.setText(descrip[index]);
            }

        }

这是我的布局文件中的 sn-ps: activity_main.xml 纵向视图:

<ImageView
            android:id="@+id/leaderPhoto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/accessability"
            android:scaleType="centerInside"
            android:src="@drawable/ic_world1" />

activity_in.xml 横向视图:

<ImageView
            android:id="@+id/leaderPhoto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="false"
            android:contentDescription="@string/accessability"
            android:paddingLeft="8dp"
            android:scaleType="center"
            android:src="@drawable/ic_world1" />

【问题讨论】:

    标签: android alignment spinner center


    【解决方案1】:

    如果您希望它水平居中,请尝试将RelativeLayout.CENTER_VERTICAL 更改为RelativeLayout.CENTER_HORIZONTAL,或者更改为RelativeLayout.CENTER_IN_PARENT 以使其在两个轴上居中。

    【讨论】:

    • Welthanks Anup,CENTER_IN_PARENT 在应用程序第一次启动时似乎可以工作,但是在选择了微调器默认位置以外的其他内容然后返回到默认位置后它不会,即图像开始居中对齐时应用程序启动,在选择某些内容时移动到左对齐,当您返回到“未选择”位置时保持左对齐。
    • 那是因为您只是在索引更改时添加新规则。添加新规则时,您必须删除旧规则。见:stackoverflow.com/a/5110767/1369222
    • 感谢 Anup,尚未解决,但您给了我一个有用的线索。欣赏。
    【解决方案2】:

    我对您的代码进行了一些更改,以解决您的问题并帮助您:

    @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
    
                // The variable index identifies the position the spinner is in.
                // TextView name, country and description... locks to the
                // TextViews defined in the activity_main.xml layout files.
                int index = parent.getSelectedItemPosition();
                TextView name = (TextView) findViewById(R.id.name);
                TextView country = (TextView) findViewById(R.id.country);
                TextView description = (TextView) findViewById(R.id.description);
    
                // Now we'll check to see if we're in the None Selected spinner
                // position. If true we'll dump the name, country and
                // description TextViews otherwise these will be shown.
    
                if (index == 0) {
    
                    image.setImageResource(imgs.getResourceId(
                            spinner1.getSelectedItemPosition(), -1));
                    // Try and centre the image when none is selected
                    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) image
                            .getLayoutParams();
                    lp.addRule(0);
                    //this will make it center according to its parent
                    lp.addRule(RelativeLayout.CENTER_IN_PARENT );
    
                    image.setLayoutParams(lp);
    
                    name.setVisibility(View.GONE);
                    country.setVisibility(View.GONE);
                    description.setVisibility(View.GONE);
    
                } else {
    
                    image.setImageResource(imgs.getResourceId(
                            spinner1.getSelectedItemPosition(), -1));
                    // Try and left align the image when the spinner is NOT in
                    // the None Selected position.
                    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) image
                            .getLayoutParams();
                    lp.addRule(0);
                    lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    image.setLayoutParams(lp);
    
                    name.setVisibility(View.VISIBLE);
                    country.setVisibility(View.VISIBLE);
                    description.setVisibility(View.VISIBLE);
    
                    name.setText(leaders[index]);
                    country.setText(states[index]);
                    description.setText(descrip[index]);
                }
    
            }
    

    更新:根据 Anup Cowkur 的话,你应该像这样使用它,看看我更新的代码: 我希望你的问题得到解决:)

    干杯,

    哈马德

    【讨论】:

    • 非常感谢哈马德。它部分有效,但请参阅我添加到 Anup Cowkur 帖子中的 cmets 详细说明了问题。
    • ok 然后删除这个 parent.getSelectedItemPosition();并仅使用当前选定项目的位置,然后它将起作用。
    【解决方案3】:

    非常感谢在 Anup 和 Hamad 的帮助下,我现在已经解决了我的问题,在不到一个小时前发布了我的 Q 后,我的印象非常深刻。下面是现在可以运行的 Java 代码:

    @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
    
                // The variable index identifies the position the spinner is in.
                // TextView name, country and description... locks to the
                // TextViews defined in the activity_main.xml layout files.
                int index = parent.getSelectedItemPosition();
                TextView name = (TextView) findViewById(R.id.name);
                TextView country = (TextView) findViewById(R.id.country);
                TextView description = (TextView) findViewById(R.id.description);
    
                // Used to set Layout Params for the ImageView
                RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) image
                        .getLayoutParams();
                // Clears the rules for when index changes
                lp.addRule(RelativeLayout.CENTER_IN_PARENT, 0);
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
    
                // Now we'll check to see if we're in the None Selected spinner
                // position. If true we'll dump the name, country and
                // description TextViews otherwise these will be shown.
    
                if (index == 0) {
    
                    name.setVisibility(View.GONE);
                    country.setVisibility(View.GONE);
                    description.setVisibility(View.GONE);
    
                    image.setImageResource(imgs.getResourceId(
                            spinner1.getSelectedItemPosition(), -1));
                    // Try and centre the image when none is selected using
                    // Layout Params
                    lp.addRule(RelativeLayout.CENTER_IN_PARENT);
                    image.setLayoutParams(lp);
    
                } else {
    
                    image.setImageResource(imgs.getResourceId(
                            spinner1.getSelectedItemPosition(), -1));
                    // Try and left align the image when the spinner is NOT in
                    // the None Selected position using Layout Params
                    lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    image.setLayoutParams(lp);
    
                    name.setVisibility(View.VISIBLE);
                    country.setVisibility(View.VISIBLE);
                    description.setVisibility(View.VISIBLE);
    
                    name.setText(leaders[index]);
                    country.setText(states[index]);
                    description.setText(descrip[index]);
    
                }
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      相关资源
      最近更新 更多