【问题标题】:How do I programmatically position an ImageView in AndroidStudio如何以编程方式在 Android Studio 中定位 ImageView
【发布时间】:2016-06-24 11:48:12
【问题描述】:

我想将我的 ImageView 放置在 Android Studio 中,这样它就可以在 Android 手机的多种屏幕尺寸中位于同一个位置(按比例)。

这是我在activity_main.xml中的代码:

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.slippysam.sooper_fly.slippysam.MainActivity"
android:background="@drawable/bgday">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxHeight="40dp"
    android:maxWidth="40dp"
    android:adjustViewBounds="true"

    android:src="@drawable/sam"
    android:id="@+id/samm"
    />
</RelativeLayout/>

这是我在Activity.java中尝试使用的一个函数

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   positionSam();

}
public void positionSam(){
    final ImageView sam = (ImageView)findViewById(R.id.samm);

    DisplayMetrics dm = getApplicationContext().getResources().getDisplayMetrics();
    float dpHeight = dm.heightPixels / dm.density;
    float dpWidth = dm.widthPixels / dm.density;

    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) sam.getLayoutParams();
    lp.leftMargin = (int) ((dpWidth * 0.5));
    lp.topMargin = (int) ((dpHeight * 1.0555));
    sam.setLayoutParams(lp);
}
}

这个函数确实改变了我的 ImageView 的位置,但是当我在产品之间切换时它的位置仍然不具体。

【问题讨论】:

  • 请包括整个布局。
  • @ChrisWilson 完成 :)
  • 请指定像gravity="center"这样的位置或者你想要的RelativeLayout。让我知道它是否有效。
  • 为什么要从 java 文件中设置图像视图,而不是您可以轻松地在 xml 文件中的布局中设置图像视图。
  • @AbdulAleemAkhund 我不想让我的 ImageView 居中我希望它从底部的高度是整个屏幕的 1/3。

标签: java android xml android-studio imageview


【解决方案1】:

在您的布局 (RelativeLayout) 中,您需要指定 ImageView 的放置位置。

例如,您可以尝试使用属性android:centerInParent="true",这样您的布局文件将如下所示:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.slippysam.sooper_fly.slippysam.MainActivity"
    android:background="@drawable/bgday">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxHeight="40dp"
        android:maxWidth="40dp"
        android:adjustViewBounds="true"
        android:centerInParent="true"
        android:src="@drawable/sam"
        android:id="@+id/samm" />

</RelativeLayout/>

通过使用centerInParent="true",您现在不需要以编程方式将ImageView 居中。 如果您仍想以编程方式执行此操作,可以向您的 LayoutParams 添加一条规则,如下所示:

RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) sam.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
sam.setLayoutParams(lp);

然后忘记为ImageView 设置边距。

要以编程方式使用它,您需要在每次显示新图像时调用 positionSam() 方法。

【讨论】:

    【解决方案2】:

    好的,所以我的第一种方法基本上是正确的;我只需要将我的RelativeLayout 编辑为:

    <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"
    android:paddingBottom="0px"
    android:paddingLeft="0px"
    android:paddingRight="0px"
    android:paddingTop="0px"
    tools:context="com.slippysam.sooper_fly.slippysam.MainActivity"
    android:background="@drawable/bgday">
    

    更改 paddingBottom paddingLeft 等有助于函数在我需要 ImageView 的位置变得更加准确。我希望这能惠及任何需要它的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 2013-01-11
      • 1970-01-01
      相关资源
      最近更新 更多