java - 如何在 Android Studio 中以编程方式定位 ImageView

标签 java android xml android-studio imageview

我想在 Android Studio 中定位我的 ImageView,以便它可以在 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 的位置,但是当我在产品之间切换时它的位置仍然不具体。

最佳答案

在布局 (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() 方法。

关于java - 如何在 Android Studio 中以编程方式定位 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35902840/

相关文章:

android - PreferenceActivity 可以自定义它处理触摸的方式吗?

android - 从设备中选择一个文件并将其上传到 webview 中加载的页面

Android ViewPropertyAnimator 和 ValueAnimator : How can I do fast-forward?

xml - XSLT : Insert new line between two elements in a template

java - 将枚举值插入 HashMap

java - 如何使用java从xml文件中的CDATA获取测试值

java - PBEKeySpec 未生成所需的输出 key 长度

java - JTable 中的 InsertRow 和自动渲染

xml - 在PowerShell中将exiftool XML读入变量

java - Xml 不使用 sax 将字符串解析为输入