【问题标题】:resizing the imageView makes it change its place调整 imageView 的大小使其改变位置
【发布时间】:2012-08-15 05:41:08
【问题描述】:

我有一个相对布局,其中包含很少的图像,如下所示。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >


    <ImageView
        android:id="@+id/image_2"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignLeft="@+id/increase"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="34dp"
        android:layout_marginTop="34dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/image_1"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignBottom="@+id/image_2"
        android:layout_marginBottom="14dp"
        android:layout_toRightOf="@+id/increase"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/image_8"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />
</RelativeLayout>

在我的 java 文件中,我需要执行一个小任务。 当我单击图像时,它的大小应该增加。 我试过了,它工作正常,这是我的代码

public class MainActivity extends Activity implements OnClickListener {

View imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageView i1 = (ImageView ) findViewById(R.id.image_1);
    ImageView i2 = (ImageView ) findViewById(R.id.image_2);
             ImageView i3 = (ImageView ) findViewById(R.id.image_8);
            i1.setOnClickListener(this);
    i2.setOnClickListener(this);
    i3.setOnClickListener(this);
}

public void onClick(View arg0) {
    Log.v("clicked ", "clicked");
    imageView = arg0;
    Toast.makeText(this, "looking to resize the image ", 1).show();
    Toast.makeText(this, "clicked 2", 1).show();

    height = imageView.getHeight();
        width = imageView.getWidth();
    height += 50;
    width += 50;
    imageView.setLayoutParams(new RelativeLayout.LayoutParams(height, width));

}   

}

但我面临一个问题。 当我单击图像时,它的位置正在改变。 考虑一下这些屏幕截图..

以下是应用程序启动时的屏幕截图

以下是单击图像视图时的屏幕截图。

谁能给我建议,这样图片就不会改变它的位置 我可以使用诸如使用网格视图之类的东西(如果可能的话。但即使我正在尝试为其添加一些拖动选项。所以任何人都可以建议我)

更新代码:

onclick 方法更新如下:

public void onClick(View arg0) {
    Log.v("clicked ", "clicked");
    imageView = arg0;
    Toast.makeText(this, "looking to resize the image ", 1).show();
    Toast.makeText(this, "clicked 2", 1).show();

    height = imageView.getHeight();
width = imageView.getWidth();
    height += 50;
    width += 50;
    RelativeLayout.LayoutParams params = new
          RelativeLayout.LayoutParams(height, width);

    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    params.setMargins( 
                imageView.getLeft()-30,imageView.getTop()-30,imageView.getRight()+30,
               imageView.getBottom()+30);
    imageView.setLayoutParams(params);



}

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    您为相对布局创建新的布局参数,并且只设置高度和宽度。因此,图像视图将默认位于父相对布局的左上角。您还需要根据需要设置对齐方式和边距。

    由于你使用的是相对布局,你需要创建新的RelativeLayout layoutParams:

    http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

    类似这样的:

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    params.setMargins(left, top, right, bottom);
    

    祝你好运……

    【讨论】:

    • 感谢您的回复。我搜索了 imageView 的对齐属性。但我没能找到它。你能再给我一些建议吗
    • 好的,我会试一试,我会回来的。但是我有一个疑问,我了解 setmargins() 的用途以及 addRule 的用途是什么?
    • addRule 使您能够指定父视图中子视图的“相对性”,例如对齐和“toTheRightOf”等。当要求相对布局绘制自身时,它首先询问它的所有子视图在层次结构中报告它们的大小以及它们希望定位的位置。为此,它传递了布局用于定位其子级的规则。请记住,对于相对布局,您必须提供规则以将任何内容放置在左上角以外的位置。一般来说,边距不是一个好方法,但有时你无法避免。
    • 感谢您的回复。我按照你的建议更新了我的代码。但我又遇到了一个问题。单击的图像视图在单击时正在移动到其他位置,并且在再次单击时再次向后移动。请多给我一些建议。
    • 不幸的是,“我没有找到解决方案”不会让任何人提供帮助,无论他们的技术知识多么全面,或者他们可能想提供多少帮助。您将必须学会提出具体的问题并提供足够的信息以便我们提供帮助。您可能会发现这是一个有用且简短的内容,请阅读:stackoverflow.com/questions/how-to-ask
    猜你喜欢
    • 2023-01-31
    • 2013-06-27
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    相关资源
    最近更新 更多