没有在视图寻呼机中获得圆形视图

时间:2015-07-08 05:23:16

标签: android

我正在使用视图寻呼机和两个布局,每个布局都有一个按钮。我想以圆圈形式显示这些按钮。截至目前,我只是长方形。以下是我的代码。

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

<shape android:shape="rectangle" >
    <gradient
        android:angle="0"
        android:endColor="@android:color/white"
        android:startColor="@android:color/white" />

    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />

    <stroke
        android:width="1dp"
        android:color="@android:color/holo_blue_dark" />
</shape>

MainActivity.java

package com.circleviewpager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.Button;

public class MainActivity extends FragmentActivity {
private ViewPager _mViewPager;
private ViewPagerAdapter _adapter;
private Button _btn1, _btn2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setUpView();
    setTab();
}

private void setUpView() {
    _mViewPager = (ViewPager) findViewById(R.id.viewPager);
    _adapter = new ViewPagerAdapter(getApplicationContext(),
            getSupportFragmentManager());
    _mViewPager.setAdapter(_adapter);
    _mViewPager.setCurrentItem(0);
    initButton();
}

private void setTab() {
    _mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageScrollStateChanged(int position) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int position) {
            // TODO Auto-generated method stub
            btnAction(position);
        }

    });

}

private void btnAction(int action) {
    switch (action) {
    case 0:
        setButton(_btn1, "1", 20, 20);
        setButton(_btn2, "", 20, 20);
        break;

    case 1:
        setButton(_btn2, "2", 20, 20);
        setButton(_btn1, "", 20, 20);
        break;
    }
}

private void initButton() {
    _btn1 = (Button) findViewById(R.id.btn1);
    _btn2 = (Button) findViewById(R.id.btn2);
    setButton(_btn1, "1", 20, 20);
    setButton(_btn2, "", 20, 20);
}

private void setButton(Button btn, String text, int h, int w) {
    btn.setWidth(w);
    btn.setHeight(h);
    btn.setText(text);
}

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<!-- ViewPager -->
<android.support.v4.view.ViewPager
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/viewPager" />
<!-- Footer -->
<include layout="@layout/footer"  />
</FrameLayout>

在footer.xml中我有两个按钮..有人可以说我在做什么错误吗?如果您需要任何其他信息,请告诉我..

2 个答案:

答案 0 :(得分:2)

尝试更改

<shape android:shape="rectangle" >

<shape android:shape="oval" >

答案 1 :(得分:1)

您可以使用FloatingActionButton来显示圆形按钮。

Here is a good example