![](/img/trans.png)
[英]Unique paths from top-left corner to bottom-right corner of grid by getting input
[英]Button not moving from top-left to bottom-right when user touch screen
我希望按鈕更改其大小並在用戶觸摸屏幕時從左上方移動到右下方。但是,當我觸摸屏幕時,我的應用會停止運行。如果我刪除了更改按鈕位置的部分代碼,一切正常,按鈕更改其位置尺寸。
這是MainActivity.java
package com.example.user.a34;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
ViewGroup main_layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_layout = (ViewGroup)findViewById(R.id.main_layout);
main_layout.setOnTouchListener(new RelativeLayout.OnTouchListener(){
public boolean onTouch(View v,MotionEvent event){
moveButton();
return true;
}
});
}
public void moveButton(){
View button1 = (View)findViewById(R.id.button1);
//Change position---------------------
RelativeLayout.LayoutParams positionRules = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
positionRules.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
positionRules.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
button1.setLayoutParams(positionRules);
//Change size-------------------------
ViewGroup.LayoutParams sizeRules = button1.getLayoutParams();
sizeRules.width = 350;
sizeRules.height = 200;
button1.setLayoutParams(sizeRules);
}
}
這是activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.a34.MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="38dp"
android:layout_marginLeft="66dp"
app:layout_constraintLeft_toLeftOf="parent" />
</android.support.constraint.ConstraintLayout>
崩潰的直接原因可能是試圖將RelativeLayout.LayoutParams
分配給ConstraintLayout
的視圖,因此應該具有ConstraintLayout.LayoutParams
類型的布局參數。
您要么需要在xml中使用RelativeLayout
,要么分配類型為ConstraintLayout.LayoutParams
按鈕布局參數。
我建議在您的xml中使用RelativeLayout
,如下所示:
<?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/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.a34.MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginTop="38dp"
android:layout_marginBottom="38dp"
android:layout_marginLeft="66dp"
android:layout_marginRight="66dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>
並如下更改您的moveButton
方法:
public void moveButton() {
View button1 = findViewById(R.id.button1);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)button1.getLayoutParams();
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
layoutParams.width = 350;
layoutParams.height = 200;
button1.setLayoutParams(layoutParams);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.