抽屉适配器填充阿拉伯语

时间:2016-10-20 06:47:58

标签: android android-layout navigation-drawer

我有这个导航抽屉适配器,当语言是英语时效果很好,但是当语言是阿拉伯语时,一些标题部分在布局中具有边距有余量。 这是我的XML:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue"
    android:orientation="horizontal">

<ImageView
    android:id="@+id/top"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:src="@drawable/whitelogo"
    android:visibility="gone" />

<LinearLayout
    android:layout_width="match_parent"
    android:paddingTop="2dp"
    android:paddingBottom="2dp"
    android:layout_marginStart="10dp"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp"
        android:text="test"
        android:textSize="12sp"
        android:background="@color/blue"
        android:textColor="@color/white" />
</LinearLayout>


</RelativeLayout>

这是我的适配器:

public class DrawerSectionedAdapter extends BaseAdapter {


// declaring our ArrayList of items
private ArrayList<DrawerObject> objects;
Activity context;

/* here we must override the constructor for ArrayAdapter
* the only variable we care about now is ArrayList<Item> objects,
* because it is the list of objects we want to display.
*/
public DrawerSectionedAdapter(Activity context, int textViewResourceId, ArrayList<DrawerObject> objects) {

    this.objects = objects;
    this.context = context;
}

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

@Override
public Object getItem(int i) {
    return objects.get(i);
}

@Override
public long getItemId(int i) {
    return 0;
}

/*
 * we are overriding the getView method here - this is what defines how each
 * list item will look.
 */
public View getView(int position, View convertView, ViewGroup parent) {

    // assign the view we are converting to a local variable
    View v = convertView;
    int type = getItemViewType(position);
    if (v == null) {
        // Inflate the layout according to the view type
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (type == 0) {
            // Inflate the layout with image
            v = inflater.inflate(R.layout.list_item, parent, false);

        } else
            v = inflater.inflate(R.layout.value_item, parent, false);
    }

    DrawerObject i = objects.get(position);
    if (i != null) {

        // This is how you obtain a reference to the TextViews.
        // These TextViews are created in the XML files we defined.
        Typeface typeface = null;
        if (MyApplication.lang.equals("en"))
            typeface = MyApplication.opensansregular;
        else
            typeface = MyApplication.droidregular;
        if (position == 1) {
            TextView tt = (TextView) v.findViewById(R.id.name);
            if (MyApplication.lang.equals("ar"))
                tt.setGravity(Gravity.RIGHT);
            else
                tt.setGravity(Gravity.LEFT);

        }
        if (position == 0) {
            ImageView top = (ImageView) v.findViewById(R.id.top);
            top.setVisibility(View.VISIBLE);
            TextView tt = (TextView) v.findViewById(R.id.name);

            tt.setVisibility(View.GONE);

            RelativeLayout lay = (RelativeLayout) v.findViewById(R.id.lay);
            lay.setBackgroundColor(context.getResources().getColor(R.color.white));
            tt.setTypeface(typeface);


        } else {
            if (type == 0) {
                ImageView top = (ImageView) v.findViewById(R.id.top);
                top.setVisibility(View.GONE);
            }
            TextView tt = (TextView) v.findViewById(R.id.name);
            //   ViewResizing.textResize(context,tt,15);
            tt.setText(i.getName());
            tt.setVisibility(View.VISIBLE);
            tt.setTypeface(typeface);
            RelativeLayout la = (RelativeLayout) v.findViewById(lay);
            if (MyApplication.lang.equals("en"))
                la.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
            else
                la.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

            if (type != 0) {
                LinearLayout lin = (LinearLayout) v.findViewById(R.id.linear);

                if (i.isLastOne())
                    lin.setBackgroundColor(context.getResources().getColor(R.color.white));
                else
                    lin.setBackgroundResource(R.drawable.undertextline);
            }

        }
        // check to see if each individual textview is null.
        // if not, assign some text!

    }

    // the view must be returned to our activity
    return v;

}

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    return (objects.get(position).isTitle()) ? 0 : 1;
}

}

这是一个截屏(参见ادراجات...) see ادراجات

请帮助

2 个答案:

答案 0 :(得分:0)

解决了它

public class DrawerSectionedAdapter extends BaseAdapter {


// declaring our ArrayList of items
private ArrayList<DrawerObject> objects;
Activity context;

/* here we must override the constructor for ArrayAdapter
* the only variable we care about now is ArrayList<Item> objects,
* because it is the list of objects we want to display.
*/
public DrawerSectionedAdapter(Activity context, int textViewResourceId, ArrayList<DrawerObject> objects) {

    this.objects = objects;
    this.context = context;
}

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

@Override
public Object getItem(int i) {
    return objects.get(i);
}

@Override
public long getItemId(int i) {
    return 0;
}

/*
 * we are overriding the getView method here - this is what defines how each
 * list item will look.
 */
public View getView(int position, View convertView, ViewGroup parent) {

    // assign the view we are converting to a local variable
    View v = convertView;
    int type = getItemViewType(position);
    if (v == null) {
        // Inflate the layout according to the view type
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (type == 0) {
            // Inflate the layout with image
            v = inflater.inflate(R.layout.list_item, parent, false);

        } else
            v = inflater.inflate(R.layout.value_item, parent, false);
    }

    DrawerObject i = objects.get(position);
    if (i != null) {

        // This is how you obtain a reference to the TextViews.
        // These TextViews are created in the XML files we defined.
        Typeface typeface = null;
        if (MyApplication.lang.equals("en"))
            typeface = MyApplication.opensansregular;
        else
            typeface = MyApplication.droidregular;
        if (position == 1) {
            TextView tt = (TextView) v.findViewById(R.id.name);
            if (MyApplication.lang.equals("ar"))
                tt.setGravity(Gravity.RIGHT);
            else
                tt.setGravity(Gravity.LEFT);

        }
        if (position == 0) {
            ImageView top = (ImageView) v.findViewById(R.id.top);
            top.setVisibility(View.VISIBLE);
            TextView tt = (TextView) v.findViewById(R.id.name);

            tt.setVisibility(View.GONE);

            RelativeLayout lay = (RelativeLayout) v.findViewById(R.id.lay);
            lay.setBackgroundColor(ContextCompat.getColor(context,R.color.white));
            tt.setTypeface(typeface);


        } else {

            if (type == 0) {
                ImageView top = (ImageView) v.findViewById(R.id.top);
                top.setVisibility(View.GONE);
                RelativeLayout lay = (RelativeLayout) v.findViewById(R.id.lay);
                lay.setBackgroundColor(ContextCompat.getColor(context,R.color.blue));
            }
            TextView tt = (TextView) v.findViewById(R.id.name);
            //   ViewResizing.textResize(context,tt,15);
            tt.setText(i.getName());
            tt.setVisibility(View.VISIBLE);
            tt.setTypeface(typeface);
            RelativeLayout la = (RelativeLayout) v.findViewById(R.id.lay);
            //la.setBackgroundColor(ContextCompat.getColor(context,R.color.blue));
            if (MyApplication.lang.equals("en"))
                la.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
            else
                la.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

            if (type != 0) {
                LinearLayout lin = (LinearLayout) v.findViewById(R.id.linear);

                if (i.isLastOne())
                    lin.setBackgroundColor(ContextCompat.getColor(context,R.color.white));
                else
                    lin.setBackgroundResource(R.drawable.undertextline);
            }

        }
        // check to see if each individual textview is null.
        // if not, assign some text!

    }

    // the view must be returned to our activity
    return v;

}

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    return (objects.get(position).isTitle()) ? 0 : 1;
}

}

答案 1 :(得分:0)

尝试使用

android:paddingStart代替android:paddingLeft
android:paddingEnd代替android:paddingRight

当您的语言从右向左(RTL)开始时,会发生此问题。因此,请尝试使用Start&amp; End代替Left&amp; Right

如果任何问题仍然存在,请告诉我。