从右到左对齐列表视图

时间:2016-05-20 10:20:05

标签: android android-layout listview alignment

我正在使用android studio进行开发。 我有一个ListView,其中的元素从左到右对齐,我想从右到左对齐它们,我该怎么办?

java类是:

public class ProductList extends ListActivity {

    ArrayList<String> list = new ArrayList<String>(Arrays.asList("במבה אוסם", "בשר טחון", "קוקה קולה שישייה", "קפה עלית", "שמיר", "מילקי","רבעיית דניאלה"));
    ArrayAdapter adapter;

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

        Button btnDel = (Button) findViewById(R.id.btnDel);
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, list);


        /** Defining a click event listener for the button "Delete" */
        View.OnClickListener listenerDel = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /** Getting the checked items from the listview */
                SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
                int itemCount = getListView().getCount();

                for(int i=itemCount-1; i >= 0; i--){
                    if(checkedItemPositions.get(i)){
                        adapter.remove(list.get(i));
                    }
                }
                checkedItemPositions.clear();
                adapter.notifyDataSetChanged();
            }
        };


        /** Setting the event listener for the delete button */
        btnDel.setOnClickListener(listenerDel);

        /** Setting the adapter to the ListView */
        setListAdapter(adapter);

    }

    public void StartCalck(View view){
        Intent intent = new Intent(ProductList.this, SplitBuying.class);
        startActivity(intent);
    }




}

,布局为:

<LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ListView
            android:id="@android:id/list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:choiceMode="multipleChoice"
            android:layout_below="@+id/ChooseStore">
        </ListView>

    </LinearLayout>

谢谢!

4 个答案:

答案 0 :(得分:0)

使用RecyclerView而不是ListView。

LinearLayoutManager(Context context, int orientation, boolean reverseLayout)

答案 1 :(得分:0)

你应该使用自定义布局 myTextView.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:gravity="right"
android:background="#bdbdbd"/>

将此布局传递给适配器,如

 ArrayAdapter  adapter = new ArrayAdapter(this,R.layout.nameOfLayout, list);

答案 2 :(得分:0)

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right" ><br>
        <TextView
            android:id="@+id/textView_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"/><br>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:layout_toLeftOf="@+id/textView_name"/>

    </RelativeLayout>

答案 3 :(得分:0)

的Manifest.xml

<application
....
 android:supportsRtl="true"
...
</application>