0
我希望能夠點擊「SizeClicker」按鈕並更改「Clicker」按鈕的大小。我是Java和Android Studios的新手。我正在開發一個應用程序。我希望能夠將「Clicker」更改爲我需要的尺寸。如果我正在做這100%錯誤,你能幫我改變大小(寬度和高度)。如何通過另一個按鈕的onClick更改按鈕的大小?
import android.app.Activity;
import android.os.Debug;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button Clicker = (Button) findViewById(R.id.Clicker);
Button Sizer = (Button) findViewById(R.id.SizeClicker);
Sizer.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
ViewGroup.LayoutParams params = Clicker.getLayoutParams();
params.height++;
params.width++;
Clicker.setLayoutParams(params);
}});
}
的XML:
<Button
style="?android:attr/buttonStyleSmall"
android:text="@string/SizeClickerText"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:id="@+id/SizeClicker"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/buttonshapesize"
android:layout_above="@+id/Clicker"
android:layout_toEndOf="@+id/Clicker" />
<Button
android:text="@string/ClickerButton"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:id="@+id/Clicker"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="@drawable/buttonshape"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/ClickerDisplayText"
android:layout_marginBottom="50dp"
android:clickable="false" />