[英]How to align imageView after change by button
我開始學習制作Android Apps,但偶然發現了一個問題。 我讓我的應用程序通過按鈕更改了imageView
button = (Button)findViewById(R.id.bttOK);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageView.setImageResource(R.drawable.ring); // changes the image
因此,現在,當我單擊按鈕時,它會更改圖像。
問題是,原始圖像居中,而替換它的圖像設置在左上方。 我無法將其居中,也無法移動它或其他任何東西。 我想我在某處缺少一個小細節。
MainActivity.java:
package com.maxa.testaplikacija;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView text;
Button button;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
button = (Button)findViewById(R.id.bttOK);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageView.setImageResource(R.drawable.ring);
}
});
}
}
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/udajSe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/bttOK"
android:layout_centerHorizontal="true"
android:text="@string/udaj_se_za_mene" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:adjustViewBounds="false"
android:contentDescription="@string/poklon"
android:foregroundGravity="center_horizontal"
app:srcCompat="@drawable/present" />
<Button
android:id="@+id/bttOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="115dp"
android:text="@string/otvori_me" />
</RelativeLayout>
我對此還不陌生,但是我是一個快速學習者。
您的視圖似乎將圖像設置為左上角,請在ImageView中查看以下XML標簽:
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
您確定原始圖像文件的間距沒有不同/圖像本身是否包含額外的填充?
在onclick方法內部,將imageView
像這樣對齊
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageView.setImageResource(R.drawable.ring);
imageView.setLayoutParams(params);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.