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

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

How to set Margins for View in android studio?

问题

  1. class MyAdapter extends ArrayAdapter<String> {
  2. // ... (rest of the code)
  3. @NonNull
  4. @Override
  5. public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  6. LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  7. View row = layoutInflater.inflate(R.layout.firstpagerowitems, parent, false);
  8. TextView images = row.findViewById(R.id.textView3);
  9. TextView myTitle = row.findViewById(R.id.textView1);
  10. TextView myDescription = row.findViewById(R.id.textView2);
  11. // ... (other code)
  12. // Modify layout parameters to set margin
  13. ViewGroup.MarginLayoutParams margins = new ViewGroup.MarginLayoutParams(row.getLayoutParams());
  14. margins.setMargins(0, 100, 0, 100);
  15. row.setLayoutParams(new ViewGroup.LayoutParams(margins));
  16. return row;
  17. }
  18. }
  1. <!-- firsypagerowitems.xml -->
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <androidx.coordinatorlayout.widget.CoordinatorLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. xmlns:tools="http://schemas.android.com/tools"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:background="@color/colorWhite"
  10. tools:context=".Firstpage">
  11. <LinearLayout
  12. android:layout_height="match_parent"
  13. android:layout_width="match_parent"
  14. android:orientation="vertical"
  15. android:id="@+id/mylinear">
  16. <ListView
  17. android:layout_width="match_parent"
  18. android:layout_height="match_parent"
  19. android:id="@+id/listView"
  20. android:layout_marginBottom="10dp"
  21. android:layout_marginTop="10dp"
  22. android:layout_marginEnd="10dp"
  23. android:layout_marginStart="10dp">
  24. </ListView>
  25. </LinearLayout>
  26. <com.google.android.material.floatingactionbutton.FloatingActionButton
  27. android:layout_height="wrap_content"
  28. android:layout_width="wrap_content"
  29. android:id="@+id/fabs"
  30. android:layout_gravity="end|bottom"
  31. android:layout_margin="16dp"
  32. app:fabSize="normal"
  33. app:backgroundTint="@color/design_default_color_error"
  34. app:elevation="6dp"
  35. android:src="@android:drawable/ic_input_add">
  36. </com.google.android.material.floatingactionbutton.FloatingActionButton>
  37. </androidx.coordinatorlayout.widget.CoordinatorLayout>
  1. <!-- code.xml -->
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:orientation="horizontal">
  7. <TextView
  8. android:layout_width="0dp"
  9. android:layout_height="wrap_content"
  10. android:text="one"
  11. android:textColor="@color/colorWhite"
  12. android:textStyle="bold"
  13. android:layout_margin="5dp"
  14. android:textSize="20sp"
  15. android:layout_weight="0.2"
  16. android:id="@+id/textView1" />
  17. <!-- ... (other TextViews) ... -->
  18. </LinearLayout>
  1. <!-- firstpage.xml -->
  2. <!-- 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.

  1. class MyAdapter extends ArrayAdapter&lt;String&gt; {
  2. Context context;
  3. ArrayList&lt;String&gt; rTitle;
  4. ArrayList&lt;String&gt; rDescription;
  5. ArrayList&lt;String&gt; rImgs;
  6. MyAdapter (Context c, ArrayList&lt;String&gt; title,ArrayList&lt;String&gt; description,ArrayList&lt;String&gt; imgs) {
  7. super(c, R.layout.firstpagerowitems, R.id.textView1, title);
  8. this.context = c;
  9. this.rTitle = title;
  10. this.rDescription = description;
  11. this.rImgs = imgs;
  12. }
  13. @NonNull
  14. @Override
  15. public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  16. LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  17. View row = layoutInflater.inflate(R.layout.firstpagerowitems, parent, false);
  18. TextView images = row.findViewById(R.id.textView3);
  19. TextView myTitle = row.findViewById(R.id.textView1);
  20. TextView myDescription = row.findViewById(R.id.textView2);
  21. Log.d(&quot;entered last&quot;,&quot;yes&quot;);
  22. Log.d(&quot;postion&quot;, String.valueOf(position));
  23. Log.d(&quot;rimgs&quot;, String.valueOf(rImgs));
  24. Log.d(&quot;desc&quot;, String.valueOf(rDescription));
  25. Log.d(&quot;title&quot;, String.valueOf(rTitle));
  26. // now set our resources on views
  27. images.setText(rImgs.get(position));
  28. myTitle.setText(rTitle.get(position));
  29. myDescription.setText(rDescription.get(position));
  30. Random random=new Random();
  31. int trp=random.nextInt(16);
  32. Log.d(&quot;enteredcolor&quot;,mycolors[trp]);
  33. // images.setBackgroundResource(R.color.lightgreen);
  34. String fd=mycolors[trp];
  35. // images.setBackgroundColor(Color.parseColor(fd));
  36. // LinearLayout mylinear=row.findViewById(R.id.mylinear);
  37. // mylinear.setBackgroundColor(Color.parseColor(fd));
  38. row.setBackgroundColor(Color.parseColor(fd));
  39. // ViewGroup.MarginLayoutParams margins=new ViewGroup.MarginLayoutParams(row.getLayoutParams());
  40. // margins.setMargins(0,100,0,100);
  41. // ViewGroup.LayoutParams layouts=new ViewGroup.LayoutParams(margins);
  42. // row.setLayoutParams(layouts);
  43. // ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(row);
  44. return row;
  45. }
  46. }

code.java

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. android:layout_width=&quot;match_parent&quot;
  4. android:layout_height=&quot;wrap_content&quot;
  5. android:orientation=&quot;horizontal&quot;&gt;
  6. &lt;TextView
  7. android:layout_width=&quot;0dp&quot;
  8. android:layout_height=&quot;wrap_content&quot;
  9. android:text=&quot;one&quot;
  10. android:textColor=&quot;@color/colorWhite&quot;
  11. android:textStyle=&quot;bold&quot;
  12. android:layout_margin=&quot;5dp&quot;
  13. android:textSize=&quot;20sp&quot;
  14. android:layout_weight=&quot;0.2&quot;
  15. android:id=&quot;@+id/textView1&quot;
  16. /&gt;
  17. &lt;TextView
  18. android:layout_width=&quot;0dp&quot;
  19. android:layout_height=&quot;wrap_content&quot;
  20. android:text=&quot;two&quot;
  21. android:textColor=&quot;@color/colorWhite&quot;
  22. android:textStyle=&quot;bold&quot;
  23. android:layout_margin=&quot;5dp&quot;
  24. android:textSize=&quot;20sp&quot;
  25. android:layout_weight=&quot;0.45&quot;
  26. android:id=&quot;@+id/textView2&quot;
  27. /&gt;
  28. &lt;TextView
  29. android:layout_width=&quot;0dp&quot;
  30. android:layout_height=&quot;wrap_content&quot;
  31. android:text=&quot;three&quot;
  32. android:textColor=&quot;@color/colorWhite&quot;
  33. android:textStyle=&quot;bold&quot;
  34. android:layout_margin=&quot;5dp&quot;
  35. android:textSize=&quot;20sp&quot;
  36. android:layout_weight=&quot;0.35&quot;
  37. android:id=&quot;@+id/textView3&quot;
  38. /&gt;
  39. &lt;/LinearLayout&gt;

firsypagerowitems.xml

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.coordinatorlayout.widget.CoordinatorLayout
  3. xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  4. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  5. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  6. android:layout_width=&quot;match_parent&quot;
  7. android:layout_height=&quot;match_parent&quot;
  8. android:background=&quot;@color/colorWhite&quot;
  9. tools:context=&quot;.Firstpage&quot;&gt;
  10. &lt;LinearLayout
  11. android:layout_height=&quot;match_parent&quot;
  12. android:layout_width=&quot;match_parent&quot;
  13. android:orientation=&quot;vertical&quot;
  14. android:id=&quot;@+id/mylinear&quot;&gt;
  15. &lt;ListView
  16. android:layout_width=&quot;match_parent&quot;
  17. android:layout_height=&quot;match_parent&quot;
  18. android:id=&quot;@+id/listView&quot;
  19. android:layout_marginBottom=&quot;10dp&quot;
  20. android:layout_marginTop=&quot;10dp&quot;
  21. android:layout_marginEnd=&quot;10dp&quot;
  22. android:layout_marginStart=&quot;10dp&quot;
  23. &gt;
  24. &lt;/ListView&gt;
  25. &lt;/LinearLayout&gt;
  26. &lt;com.google.android.material.floatingactionbutton.FloatingActionButton
  27. android:layout_height=&quot;wrap_content&quot;
  28. android:layout_width=&quot;wrap_content&quot;
  29. android:id=&quot;@+id/fabs&quot;
  30. android:layout_gravity=&quot;end|bottom&quot;
  31. android:layout_margin=&quot;16dp&quot;
  32. app:fabSize=&quot;normal&quot;
  33. app:backgroundTint=&quot;@color/design_default_color_error&quot;
  34. app:elevation=&quot;6dp&quot;
  35. android:src=&quot;@android:drawable/ic_input_add&quot;
  36. &gt;
  37. &lt;/com.google.android.material.floatingactionbutton.FloatingActionButton&gt;
  38. &lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

firstpage.xml

答案1

得分: 1

  1. 尝试看这是否有帮助...
  2. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  3. &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  4. android:layout_width=&quot;match_parent&quot;
  5. android:layout_height=&quot;wrap_content&quot;
  6. android:orientation=&quot;horizontal&quot;&gt;
  7. &lt;TextView
  8. android:layout_width=&quot;wrap_content&quot;
  9. android:layout_height=&quot;wrap_content&quot;
  10. android:text=&quot;one&quot;
  11. android:layout_marginBottom=&quot;5dp&quot;
  12. android:textColor=&quot;@color/colorWhite&quot;
  13. android:textStyle=&quot;bold&quot;
  14. android:textSize=&quot;20sp&quot;
  15. android:layout_weight=&quot;0.2&quot;
  16. android:id=&quot;@+id/textView1&quot;
  17. /&gt;
  18. &lt;TextView
  19. android:layout_width=&quot;wrap_content&quot;
  20. android:layout_height=&quot;wrap_content&quot;
  21. android:text=&quot;two&quot;
  22. android:textColor=&quot;@color/colorWhite&quot;
  23. android:textStyle=&quot;bold&quot;
  24. android:layout_marginBottom=&quot;5dp&quot;
  25. android:textSize=&quot;20sp&quot;
  26. android:layout_weight=&quot;0.45&quot;
  27. android:id=&quot;@+id/textView2&quot;
  28. /&gt;
  29. &lt;TextView
  30. android:layout_width=&quot;wrap_content&quot;
  31. android:layout_height=&quot;wrap_content&quot;
  32. android:text=&quot;three&quot;
  33. android:textColor=&quot;@color/colorWhite&quot;
  34. android:textStyle=&quot;bold&quot;
  35. android:textSize=&quot;20sp&quot;
  36. android:layout_weight=&quot;0.35&quot;
  37. android:id=&quot;@+id/textView3&quot;
  38. /&gt;
  39. &lt;/LinearLayout&gt;

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

英文:

Try whether this helps...

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. android:layout_width=&quot;match_parent&quot;
  4. android:layout_height=&quot;wrap_content&quot;
  5. android:orientation=&quot;horizontal&quot;&gt;
  6. &lt;TextView
  7. android:layout_width=&quot;wrap_content&quot;
  8. android:layout_height=&quot;wrap_content&quot;
  9. android:text=&quot;one&quot;
  10. android:layout_marginBottom=&quot;5dp&quot;
  11. android:textColor=&quot;@color/colorWhite&quot;
  12. android:textStyle=&quot;bold&quot;
  13. android:textSize=&quot;20sp&quot;
  14. android:layout_weight=&quot;0.2&quot;
  15. android:id=&quot;@+id/textView1&quot;
  16. /&gt;
  17. &lt;TextView
  18. android:layout_width=&quot;wrap_content&quot;
  19. android:layout_height=&quot;wrap_content&quot;
  20. android:text=&quot;two&quot;
  21. android:textColor=&quot;@color/colorWhite&quot;
  22. android:textStyle=&quot;bold&quot;
  23. android:layout_marginBottom=&quot;5dp&quot;
  24. android:textSize=&quot;20sp&quot;
  25. android:layout_weight=&quot;0.45&quot;
  26. android:id=&quot;@+id/textView2&quot;
  27. /&gt;
  28. &lt;TextView
  29. android:layout_width=&quot;wrap_content&quot;
  30. android:layout_height=&quot;wrap_content&quot;
  31. android:text=&quot;three&quot;
  32. android:textColor=&quot;@color/colorWhite&quot;
  33. android:textStyle=&quot;bold&quot;
  34. android:textSize=&quot;20sp&quot;
  35. android:layout_weight=&quot;0.35&quot;
  36. android:id=&quot;@+id/textView3&quot;
  37. /&gt;
  38. &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:

确定