如何在Android中以编程方式设置左右边距

时间:2015-01-21 11:34:24

标签: android

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/even_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1" >

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.20" >

            <com.lociiapp.utils.RoundedImageView
                android:id="@+id/odd_bubble"
                android:layout_width="60dip"
                android:layout_height="60dip"
                android:layout_margin="5dip"
                android:visibility="visible" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.80" >

            <LinearLayout
                android:id="@+id/shareRow"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.9"
                android:background="#07000000"
                android:orientation="horizontal" >

                <ImageView
                    android:id="@+id/left"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dip"
                    android:layout_marginTop="15dip"
                    android:layout_weight=".10"
                    android:src="@drawable/callout_left" />

                <TextView
                    android:id="@+id/text"
                    android:layout_width="200dip"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dip"
                    android:layout_marginTop="15dip"
                    android:layout_weight="0.52"
                    android:textColor="#636363"
                    android:textSize="20sp" />

                <ImageView
                    android:id="@+id/right"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dip"
                    android:layout_marginTop="15dip"
                    android:layout_toRightOf="@+id/text"
                    android:layout_weight=".10"
                    android:src="@drawable/callout_right" />
            </LinearLayout>
        </RelativeLayout>

    </LinearLayout>

AdapterClass:

 public class ChatAdapter extends ArrayAdapter<Chat> {
        private final Context context;
        private final ArrayList<Chat> values;
        ImageLoader imageloader;
        Datamodel dm;

        public ChatAdapter(Context context, ArrayList<Chat> values) {
            super(context, R.layout.list_row_layout_even, values);
            // TODO Auto-generated constructor stub
            this.context = context;
            this.values = values;
    //      imageloader = new ImageLoader(context);

        }

        public void addMessage(Chat chat) {
            values.add(chat);
            this.notifyDataSetChanged();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null)
                convertView = inflater.inflate(R.layout.list_row_layout_odd,
                        parent, false);

            LinearLayout root = (LinearLayout) convertView
                    .findViewById(R.id.even_container);
            TextView tv = (TextView) convertView.findViewById(R.id.text);

            RoundedImageView oddImg = (RoundedImageView) convertView
                    .findViewById(R.id.odd_bubble);

            ImageView leftimageicon = (ImageView) root.findViewById(R.id.left);
            ImageView rightimageicon = (ImageView) root.findViewById(R.id.right);

            Typeface fontArial = Typeface.createFromAsset(context.getAssets(),
                    "fonts/ARIAL.TTF");

            SharedPreferences prefs = context.getSharedPreferences(
                    AppConstants.LOGIN_PREFS, Context.MODE_PRIVATE);
            String K = prefs.getString("Member_id", "");
            Chat chat = values.get(position);

            String t = chat.getRecieverID();

            tv.setText(chat.getMessage());

            tv.setTypeface(fontArial);

            AQuery aq = new AQuery(context);

            if (chat.getSenderID().equals(prefs.getString("Member_id", ""))) {
                root.setBackgroundColor(Color.parseColor("#07000000"));
                tv.setTextColor(Color.parseColor("#636363"));

                tv.setBackgroundColor(Color.parseColor("#00ad9a"));
                leftimageicon.setVisibility(View.GONE);
                rightimageicon.setVisibility(View.VISIBLE);
                oddImg.setVisibility(View.GONE);
            } else {
                root.setBackgroundColor(Color.parseColor("#07000000"));
                tv.setTextColor(Color.parseColor("#bdbdbd"));
                tv.setBackgroundColor(Color.parseColor("#ffffff"));
                oddImg.setVisibility(View.VISIBLE);
                aq.id(oddImg).image(
                        "http://api.lociiapp.com/TransientStorage/" + K + ".jpg");
                leftimageicon.setVisibility(View.VISIBLE);
                rightimageicon.setVisibility(View.GONE);
                oddImg.setVisibility(View.VISIBLE);

            }

            return convertView;
        }
    }

这是我的适配器类我有textview,其id是文本我想要设置textview的左右边缘编程我的意思是说,如果它会调整,那么保证金应该离开,如果正确,那么保证金应该正确请帮助我,我将如何这是我试图实现但不理解

2 个答案:

答案 0 :(得分:1)

请用此替换你的getView方法:

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null)
                convertView = inflater.inflate(R.layout.list_row_layout_odd,
                        parent, false);

            LinearLayout root = (LinearLayout) convertView
                    .findViewById(R.id.even_container);
            TextView tv = (TextView) convertView.findViewById(R.id.text);

            RoundedImageView oddImg = (RoundedImageView) convertView
                    .findViewById(R.id.odd_bubble);

            ImageView leftimageicon = (ImageView) root.findViewById(R.id.left);
            ImageView rightimageicon = (ImageView) root.findViewById(R.id.right);

            Typeface fontArial = Typeface.createFromAsset(context.getAssets(),
                    "fonts/ARIAL.TTF");

            SharedPreferences prefs = context.getSharedPreferences(
                    AppConstants.LOGIN_PREFS, Context.MODE_PRIVATE);
            String K = prefs.getString("Member_id", "");
            Chat chat = values.get(position);

            String t = chat.getRecieverID();

            tv.setText(chat.getMessage());

            tv.setTypeface(fontArial);

            AQuery aq = new AQuery(context);

            if (chat.getSenderID().equals(prefs.getString("Member_id", ""))) {
                root.setBackgroundColor(Color.parseColor("#07000000"));
                tv.setTextColor(Color.parseColor("#636363"));

                //setting left margin 50
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tv.getLayoutParams();
                params.setMargins(50, 0, 0, 0);

                tv.setLayoutParams(params);

                tv.setBackgroundColor(Color.parseColor("#00ad9a"));
                leftimageicon.setVisibility(View.GONE);
                rightimageicon.setVisibility(View.VISIBLE);
                oddImg.setVisibility(View.GONE);
            } else {
                root.setBackgroundColor(Color.parseColor("#07000000"));
                tv.setTextColor(Color.parseColor("#bdbdbd"));

                //setting right margin 50
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tv.getLayoutParams();
                params.setMargins(0, 0, 50, 0);

                tv.setLayoutParams(params);

                tv.setBackgroundColor(Color.parseColor("#ffffff"));
                oddImg.setVisibility(View.VISIBLE);
                aq.id(oddImg).image(
                        "http://api.lociiapp.com/TransientStorage/" + K + ".jpg");
                leftimageicon.setVisibility(View.VISIBLE);
                rightimageicon.setVisibility(View.GONE);
                oddImg.setVisibility(View.VISIBLE);

            }

            return convertView;
        }

享受

答案 1 :(得分:-1)

您可以使用以下代码设置textview的边距:

    TextView textview= (TextView) findViewById(R.id.text);

    LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    llp.setMargins(50, 0, 50, 0); // llp.setMargins(left, top, right, bottom);
    textview.setLayoutParams(llp);
相关问题