删除动态Textviews Android中的顶部和底部边距

时间:2015-02-23 10:29:44

标签: android textview android-linearlayout center gravity

如何删除绿色LinearLayout中点的顶部和底部边距?

我已尝试将setMargins设为0但不起作用

这是我的Java类和我的XML

enter image description here

MainActivity.java

public class MainActivity extends Activity {

private ViewPager viewPager;
private MyViewPagerAdapter myViewPagerAdapter;
private ArrayList<Integer> listOfItems;

private LinearLayout dotsLayout;
private int dotsCount;
private TextView[] dots;

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

    initViews();
    setViewPagerItemsWithAdapter();
    setUiPageViewController();
}

private void setUiPageViewController() {
    dotsLayout = (LinearLayout)findViewById(R.id.viewPagerCountDots);
    dotsCount = myViewPagerAdapter.getCount();
    dots = new TextView[dotsCount];

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.CENTER_VERTICAL;
    params.setMargins(2, 0, 1, 0);

    for (int i = 0; i < dotsCount; i++) {
        dots[i] = new TextView(this);
        dots[i].setText(Html.fromHtml("&#8226;"));
        dots[i].setTextSize(30);
        dots[i].setTextColor(getResources().getColor(R.color.blanco));
        dots[i].setLayoutParams(params);
        dotsLayout.addView(dots[i]);            
    }

    dots[0].setTextColor(getResources().getColor(R.color.negro));
}

private void setViewPagerItemsWithAdapter() {
    myViewPagerAdapter = new MyViewPagerAdapter(listOfItems);
    viewPager.setAdapter(myViewPagerAdapter);
    viewPager.setCurrentItem(0);
    viewPager.setOnPageChangeListener(viewPagerPageChangeListener);
}

//  page change listener
OnPageChangeListener viewPagerPageChangeListener = new OnPageChangeListener() {

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < dotsCount; i++) {
            dots[i].setTextColor(getResources().getColor(R.color.blanco));
        }
        dots[position].setTextColor(getResources().getColor(R.color.negro));
    }

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

    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }
};

private void initViews() {

    viewPager = (ViewPager)findViewById(R.id.viewPager);

    listOfItems = new ArrayList<Integer>();
    listOfItems.add(1);
    listOfItems.add(2);
    listOfItems.add(3);
    listOfItems.add(4);
    listOfItems.add(5);
}

//  adapter
public class MyViewPagerAdapter extends PagerAdapter{

    private LayoutInflater layoutInflater;
    private ArrayList<Integer> items;

    public MyViewPagerAdapter(ArrayList<Integer> listOfItems) {
        this.items = listOfItems;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.view_pager_item, container,false);

        TextView tView = (TextView)view.findViewById(R.id.PageNumber);

        tView.setText(listOfItems.get(position).toString());

        ((ViewPager) container).addView(view);

        return view;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object obj) {
        return view == ((View)obj);
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View)object;
        ((ViewPager) container).removeView(view);
    }
}
}

activity_main.xml中

<RelativeLayout 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.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="100dp" >
</android.support.v4.view.ViewPager>

<RelativeLayout
    android:id="@+id/viewPagerIndicator"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/viewPager"
    android:background="@color/app_green" >

    <LinearLayout
        android:id="@+id/viewPagerCountDots"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="horizontal" >
    </LinearLayout>
</RelativeLayout>

</RelativeLayout>

view_pager_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000" >

<TextView
    android:id="@+id/PageNumber"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:textSize="50sp" />

</RelativeLayout>

5 个答案:

答案 0 :(得分:0)

不要在布局中使用WrapContent高度和宽度,android重力属性不适用于wrapcontent高度和宽度

使用此

 <LinearLayout
            android:id="@+id/viewPagerCountDots"
           android:layout_width="match_parent"
            android:layout_height="20dp"
            android:gravity="center"
            android:orientation="horizontal">

答案 1 :(得分:0)

您可以尝试从DDMS尝试转储您的视图层次结构。

虽然我不确定,但对于textview点可能Match Parent会有所帮助。

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
    params.gravity = Gravity.CENTER;

    for (int i = 0; i < dotsCount; i++) {
        dots[i] = new TextView(this);
        dots[i].setText(Html.fromHtml("&#8226;"));
        dots[i].setTextSize(30);
        dots[i].setTextColor(getResources().getColor(R.color.blanco));
        dots[i].setLayoutParams(params);
        dotsLayout.addView(dots[i]);            
    }

答案 2 :(得分:0)

将LinearLayout宽度设置为MATCH_PARENT,因为对于中心的点使用LinearLayout,LinearLayout必须打开宽度。

答案 3 :(得分:0)

对不起,我发现你的回答后我已经看到你编辑了你的问题。使用此功能,您可以将点置于中心位置:将文本的大小更改为较小的值

dots[i].setTextSize(20);

使用当前值(30),点数更大,但TextView也更大。他们比父母高,这就是为什么他们不看中心

答案 4 :(得分:0)

根据我对你如何做事的理解,问题是因为你使用句号字符作为指标。因此,文本视图可以容纳更大的字符。

我建议使用白色和黑色圆圈的图像并将它们放在线性布局中。或者您可以使用下面的RadioGroup

<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:orientation="horizontal">

<RadioButton
    android:id="@+id/radio0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="" />

<RadioButton
    android:id="@+id/radio1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />

<RadioButton
    android:id="@+id/radio2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />
</RadioGroup>

然后设置哪个显示为选中使用类似

的内容
RadioGroup dots = (RadioGroup)findViewById(R.id.radioGroup1);
int[] dotsIndex = {r.id.radio0, r.id.radio1,r.id.radio2};
dots.check(dotsIndex[indexToBeHighlighted]);