删除动态文本视图中的顶部和底部边距 Android

Delete top and bottom margins in dynamic Textviews Android

如何删除绿色LinearLayout中圆点的上下边距?

我试过将 setMargins 设置为 0 但它不起作用

这是我的 Java class 和我的 XML 的

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>

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

使用这个

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

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

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

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]);            
    }

将您的 LinearLayout 宽度设置为 MATCH_PARENT,因为您正在为中心的点使用 LinearLayout LinearLayout 必须具有完整的宽度。

抱歉,我发现您在发布我的回答后编辑了您的问题。有了这个你可以让点居中:将文本的大小更改为较小的值

dots[i].setTextSize(20);

您当前的值 (30) 点更大,但 TextView 也更大。它们比 parent 高,这就是它们看起来不居中的原因

据我了解您的工作方式,问题是因为您使用句号作为指示符。所以文本视图有 space 以容纳更大的字符。

我建议使用白色和黑色圆圈的图像并将它们放在线性布局中。或者您可以使用如下所示的 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]);