【问题标题】:How to set left and right margin programmatically in Android [duplicate]如何在Android中以编程方式设置左右边距[重复]
【发布时间】:2019-01-15 11:26:55
【问题描述】:
  <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>

适配器类:

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

这是我的适配器类我有文本视图,其 id 是文本我想设置文本视图的左右边距以编程方式我的意思是说如果它会均匀标注,那么边距应该在左边,或者如果右边那么边距应该在右边请帮助我如何ac-chive 这是我正在尝试实现但不理解的

【问题讨论】:

    标签: android


    【解决方案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;
            }
    

    享受

    【讨论】:

    • 记住 params.setMargins(left, top, right, bottom);可以根据需要进行调整。
    • 谢谢我能有你的电子邮件ID plz
    【解决方案2】:

    您可以使用以下代码为您的文本视图设置边距:

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

    【讨论】:

    • 你有足够的声望来关闭这个重复的问题......现在,你只是窃取某人的答案并重新发布为你的......
    猜你喜欢
    • 2011-11-22
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2016-10-11
    • 2012-09-25
    • 2015-02-24
    相关资源
    最近更新 更多