0

我需要创建一个有 5 个半径按钮的程序。当我单击这些每个按钮时,我想调整我的圆半径。(圆在所有 android 手机中应该具有相同的大小)。请帮我找到这个...

4

2 回答 2

0

You could create a circle-shape in xml and set this as background resource to a button or an imageButton, or you could do your own button-class and override onDraw method. A tutorial with shape is here:

http://dandar3.blogspot.de/2012/10/android-custom-round-buttons.html

or here:

http://yekmer.posterous.com/how-to-make-rounded-buttons-on-android

Instead of using "rectangle" at the shape, You could use "oval".

I don´t know if I understand You right, but making a button that had the same size on every phone is not a good way. Views had to be independent, a view should be created in a way, that it is adjusted to the individual screen size. To do so, use "dp" units in your xml layouts.

Now here is my example:

1.) first create a shape-drawable in the drawable folder:

round_button_oval_shape.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval" >
        <solid android:color="#00ced1" />
    </shape>

2.) create a second shape in drawable folder:

rounded_button_oval_shape_pressed.xml

     <?xml version="1.0" encoding="utf-8"?>
     <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="oval" >
          <solid android:color="#008b8b" />
     </shape>

3.) create a selector in the drawable folder:

rounded_button_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">

       <item android:drawable="@drawable/rounded_button_oval_shape_pressed"
       android:state_pressed="true"></item>
       <item android:drawable="@drawable/round_button_oval_shape"
       android:state_pressed="false"></item>
       <item android:drawable="@drawable/round_button_oval_shape"></item>
    </selector>

4.)create your main-layout

main.xml

    <LinearLayout 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:background="#000000"
       android:gravity="center"
       android:orientation="vertical" >

     <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/hello_world"
       android:textColor="#ff0000" />

     <Button
       android:id="@+id/rounded_button"
       android:layout_width="250dp"
       android:layout_height="250dp"
       android:background="@drawable/rounded_button_selector" />

     </LinearLayout>

If you have done this parts, you could anything do with that button in your activity.

RoundedButtonDemo.java

    public class RoundedButtonDemo extends Activity {

private Button mRoundedButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mRoundedButton = (Button) findViewById(R.id.rounded_button); //initialize your button
    mRoundedButton.setOnClickListener(new OnClickListener() {   // set Button on click listener

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(RoundedButtonDemo.this,            //show a toast when pressing button
                    R.string.rounded_button_message, Toast.LENGTH_LONG)
                    .show();
        }

    });
   }

    }

The shapes and the selector are needed for showing a pressed behavior of the button. The first shape is a normal button, which is not pressed. the second one is a shape that is pressed. The selector is using the two shapes for showing the pressed state to the user. To get this, set the selector as background to your button in your main.xml .

于 2012-11-19T06:03:52.687 回答
0

在创建圆圈并以此为基础采取行动之前扣除屏幕尺寸是一种优雅的方式,

安卓设备的屏幕大小不同,

所以找出屏幕尺寸根据找到的屏幕尺寸放置您的坐标值,

如果绘图只是您的一种选择,那么以下线程将有助于您实现这一目标,

兼容的画布绘制提示

如果不是,那么按钮背景应该是 9 补丁图像总是更好。

希望,你会发现它很有用。

于 2012-11-19T08:00:22.360 回答