英文:
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<String> {
Context context;
ArrayList<String> rTitle;
ArrayList<String> rDescription;
ArrayList<String> rImgs;
MyAdapter (Context c, ArrayList<String> title,ArrayList<String> description,ArrayList<String> 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("entered last","yes");
Log.d("postion", String.valueOf(position));
Log.d("rimgs", String.valueOf(rImgs));
Log.d("desc", String.valueOf(rDescription));
Log.d("title", 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("enteredcolor",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
<?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"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="two"
android:textColor="@color/colorWhite"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"
android:layout_weight="0.45"
android:id="@+id/textView2"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="three"
android:textColor="@color/colorWhite"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"
android:layout_weight="0.35"
android:id="@+id/textView3"
/>
</LinearLayout>
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>
firstpage.xml
答案1
得分: 1
尝试看这是否有帮助...
<?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="wrap_content"
android:layout_height="wrap_content"
android:text="one"
android:layout_marginBottom="5dp"
android:textColor="@color/colorWhite"
android:textStyle="bold"
android:textSize="20sp"
android:layout_weight="0.2"
android:id="@+id/textView1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"
android:textColor="@color/colorWhite"
android:textStyle="bold"
android:layout_marginBottom="5dp"
android:textSize="20sp"
android:layout_weight="0.45"
android:id="@+id/textView2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three"
android:textColor="@color/colorWhite"
android:textStyle="bold"
android:textSize="20sp"
android:layout_weight="0.35"
android:id="@+id/textView3"
/>
</LinearLayout>
希望这对您有所帮助。如有疑问,请随时询问...
英文:
Try whether this helps...
<?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="wrap_content"
android:layout_height="wrap_content"
android:text="one"
android:layout_marginBottom="5dp"
android:textColor="@color/colorWhite"
android:textStyle="bold"
android:textSize="20sp"
android:layout_weight="0.2"
android:id="@+id/textView1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"
android:textColor="@color/colorWhite"
android:textStyle="bold"
android:layout_marginBottom="5dp"
android:textSize="20sp"
android:layout_weight="0.45"
android:id="@+id/textView2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three"
android:textColor="@color/colorWhite"
android:textStyle="bold"
android:textSize="20sp"
android:layout_weight="0.35"
android:id="@+id/textView3"
/>
</LinearLayout>
Hope this helps. Feel free to ask for clarifications...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论