如何在Android Studio中为View设置边距?

huangapple go评论75阅读模式
英文:

How to set Margins for View in android studio?

问题

class MyAdapter extends ArrayAdapter<String> {

        // ... (rest of the code)

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = layoutInflater.inflate(R.layout.firstpagerowitems, parent, false);
            TextView images = row.findViewById(R.id.textView3);
            TextView myTitle = row.findViewById(R.id.textView1);
            TextView myDescription = row.findViewById(R.id.textView2);
            
            // ... (other code)

            // Modify layout parameters to set margin
            ViewGroup.MarginLayoutParams margins = new ViewGroup.MarginLayoutParams(row.getLayoutParams());
            margins.setMargins(0, 100, 0, 100);
            row.setLayoutParams(new ViewGroup.LayoutParams(margins));

            return row;
        }
    }
<!-- firsypagerowitems.xml -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    tools:context=".Firstpage">

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:id="@+id/mylinear">
            <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/listView"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginEnd="10dp"
                android:layout_marginStart="10dp">
            </ListView>
    </LinearLayout>
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/fabs"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"
        app:fabSize="normal"
        app:backgroundTint="@color/design_default_color_error"
        app:elevation="6dp"
        android:src="@android:drawable/ic_input_add">
    </com.google.android.material.floatingactionbutton.FloatingActionButton>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
<!-- code.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="one"
        android:textColor="@color/colorWhite"
        android:textStyle="bold"
        android:layout_margin="5dp"
        android:textSize="20sp"
        android:layout_weight="0.2"
        android:id="@+id/textView1" />
    <!-- ... (other TextViews) ... -->

</LinearLayout>
<!-- firstpage.xml -->
<!-- This part is missing in your provided content. -->
英文:

I have a view in an activity as below .I want to set Margin to the view.But i used Viewgroup.LayoutParams but it doesn't change the view of the row.Below i have given custom adapter which takes data from firstpagerowitems.xml.I have given the java code for adapter .I tried doing android:layout_marginbottom="10dp" for firstpagerowitems.xml and firstpage.xml but it doesnt work.

class MyAdapter extends ArrayAdapter&lt;String&gt; {

        Context context;
        ArrayList&lt;String&gt; rTitle;
        ArrayList&lt;String&gt; rDescription;
        ArrayList&lt;String&gt; rImgs;

        MyAdapter (Context c, ArrayList&lt;String&gt; title,ArrayList&lt;String&gt; description,ArrayList&lt;String&gt; imgs) {
            super(c, R.layout.firstpagerowitems, R.id.textView1, title);
            this.context = c;
            this.rTitle = title;
            this.rDescription = description;
            this.rImgs = imgs;

        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = layoutInflater.inflate(R.layout.firstpagerowitems, parent, false);
            TextView images = row.findViewById(R.id.textView3);
            TextView myTitle = row.findViewById(R.id.textView1);
            TextView myDescription = row.findViewById(R.id.textView2);
            Log.d(&quot;entered last&quot;,&quot;yes&quot;);
            Log.d(&quot;postion&quot;, String.valueOf(position));
            Log.d(&quot;rimgs&quot;, String.valueOf(rImgs));
            Log.d(&quot;desc&quot;, String.valueOf(rDescription));
            Log.d(&quot;title&quot;, String.valueOf(rTitle));

            // now set our resources on views
            images.setText(rImgs.get(position));
            myTitle.setText(rTitle.get(position));
            myDescription.setText(rDescription.get(position));
            Random random=new Random();
            int trp=random.nextInt(16);
            Log.d(&quot;enteredcolor&quot;,mycolors[trp]);
//            images.setBackgroundResource(R.color.lightgreen);
            String fd=mycolors[trp];
//            images.setBackgroundColor(Color.parseColor(fd));
//            LinearLayout mylinear=row.findViewById(R.id.mylinear);
//            mylinear.setBackgroundColor(Color.parseColor(fd));
            row.setBackgroundColor(Color.parseColor(fd));
//            ViewGroup.MarginLayoutParams margins=new ViewGroup.MarginLayoutParams(row.getLayoutParams());
//            margins.setMargins(0,100,0,100);
//            ViewGroup.LayoutParams layouts=new ViewGroup.LayoutParams(margins);
//            row.setLayoutParams(layouts);
//            ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(row);




            return row;
        }
    }

code.java

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:orientation=&quot;horizontal&quot;&gt;

    &lt;TextView
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;one&quot;
        android:textColor=&quot;@color/colorWhite&quot;
        android:textStyle=&quot;bold&quot;
        android:layout_margin=&quot;5dp&quot;
        android:textSize=&quot;20sp&quot;
        android:layout_weight=&quot;0.2&quot;
        android:id=&quot;@+id/textView1&quot;
        /&gt;
    &lt;TextView
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;two&quot;
        android:textColor=&quot;@color/colorWhite&quot;
        android:textStyle=&quot;bold&quot;
        android:layout_margin=&quot;5dp&quot;
        android:textSize=&quot;20sp&quot;
        android:layout_weight=&quot;0.45&quot;
        android:id=&quot;@+id/textView2&quot;
        /&gt;
    &lt;TextView
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;three&quot;
        android:textColor=&quot;@color/colorWhite&quot;
        android:textStyle=&quot;bold&quot;
        android:layout_margin=&quot;5dp&quot;
        android:textSize=&quot;20sp&quot;
        android:layout_weight=&quot;0.35&quot;
        android:id=&quot;@+id/textView3&quot;
        /&gt;
&lt;/LinearLayout&gt;

firsypagerowitems.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:background=&quot;@color/colorWhite&quot;
    tools:context=&quot;.Firstpage&quot;&gt;

    &lt;LinearLayout
        android:layout_height=&quot;match_parent&quot;
        android:layout_width=&quot;match_parent&quot;
        android:orientation=&quot;vertical&quot;
        android:id=&quot;@+id/mylinear&quot;&gt;
            &lt;ListView
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;match_parent&quot;
                android:id=&quot;@+id/listView&quot;
                android:layout_marginBottom=&quot;10dp&quot;
                android:layout_marginTop=&quot;10dp&quot;
                android:layout_marginEnd=&quot;10dp&quot;
                android:layout_marginStart=&quot;10dp&quot;
                &gt;
            &lt;/ListView&gt;
    &lt;/LinearLayout&gt;
    &lt;com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_height=&quot;wrap_content&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:id=&quot;@+id/fabs&quot;
        android:layout_gravity=&quot;end|bottom&quot;
        android:layout_margin=&quot;16dp&quot;
        app:fabSize=&quot;normal&quot;
        app:backgroundTint=&quot;@color/design_default_color_error&quot;
        app:elevation=&quot;6dp&quot;
        android:src=&quot;@android:drawable/ic_input_add&quot;
        &gt;
    &lt;/com.google.android.material.floatingactionbutton.FloatingActionButton&gt;


&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

firstpage.xml

答案1

得分: 1

尝试看这是否有帮助...

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:orientation=&quot;horizontal&quot;&gt;

    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;one&quot;
        android:layout_marginBottom=&quot;5dp&quot;
        android:textColor=&quot;@color/colorWhite&quot;
        android:textStyle=&quot;bold&quot;
        android:textSize=&quot;20sp&quot;
        android:layout_weight=&quot;0.2&quot;
        android:id=&quot;@+id/textView1&quot;
        /&gt;
    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;two&quot;
        android:textColor=&quot;@color/colorWhite&quot;
        android:textStyle=&quot;bold&quot;
        android:layout_marginBottom=&quot;5dp&quot;
        android:textSize=&quot;20sp&quot;
        android:layout_weight=&quot;0.45&quot;
        android:id=&quot;@+id/textView2&quot;
        /&gt;
    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;three&quot;
        android:textColor=&quot;@color/colorWhite&quot;
        android:textStyle=&quot;bold&quot;
        android:textSize=&quot;20sp&quot;
        android:layout_weight=&quot;0.35&quot;
        android:id=&quot;@+id/textView3&quot;
        /&gt;
&lt;/LinearLayout&gt;

希望这对您有所帮助。如有疑问,请随时询问...

英文:

Try whether this helps...

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:orientation=&quot;horizontal&quot;&gt;

    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;one&quot;
        android:layout_marginBottom=&quot;5dp&quot;
        android:textColor=&quot;@color/colorWhite&quot;
        android:textStyle=&quot;bold&quot;
        android:textSize=&quot;20sp&quot;
        android:layout_weight=&quot;0.2&quot;
        android:id=&quot;@+id/textView1&quot;
        /&gt;
    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;two&quot;
        android:textColor=&quot;@color/colorWhite&quot;
        android:textStyle=&quot;bold&quot;
        android:layout_marginBottom=&quot;5dp&quot;
        android:textSize=&quot;20sp&quot;
        android:layout_weight=&quot;0.45&quot;
        android:id=&quot;@+id/textView2&quot;
        /&gt;
    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;three&quot;
        android:textColor=&quot;@color/colorWhite&quot;
        android:textStyle=&quot;bold&quot;
        android:textSize=&quot;20sp&quot;
        android:layout_weight=&quot;0.35&quot;
        android:id=&quot;@+id/textView3&quot;
        /&gt;
&lt;/LinearLayout&gt;

Hope this helps. Feel free to ask for clarifications...

huangapple
  • 本文由 发表于 2020年7月26日 23:58:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63102494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定