簡體   English   中英

如何在AndroidStudio中以編程方式放置ImageView

[英]How do I programmatically position an ImageView in AndroidStudio

我想將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的位置,但是當我在產品之間切換時,它的位置仍然不具體。

在您的布局( 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"您無需像現在一樣以編程方式居中centerInParent="true" ImageView 如果您仍然想以編程方式執行此操作,則可以將規則添加到LayoutParams如下所示:

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

然后忘記設置ImageView的邊距。

要以編程方式使用它,每次顯示新圖像時,都需要調用positionSam()方法。

好吧,所以我的第一種方法基本上是正確的; 我只需要將我的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的地方變得更加准確。 我希望這能到達需要它的任何人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM