如何在程序中以编程方式设置ImageView的圆角?

huangapple 未分类评论77阅读模式
英文:

How to set ImageView rounded corners programmatically?

问题

我是Android Studio的新手。我想问一下,我是否可以通过编程方式创建具有圆角的ImageView?因为我想使用它来从数据库加载数据。所以,用户的个人资料图片将显示为圆角。

我的XML如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. android:background="#F3FFFC">
  9. <ScrollView
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent">
  12. <LinearLayout
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content">
  15. <ImageView
  16. android:id="@+id/imgView"
  17. android:layout_width="100dp"
  18. android:layout_height="100dp"
  19. android:src="@drawable/profilepicture"
  20. />
  21. </LinearLayout>
  22. </ScrollView>
  23. </LinearLayout>

谢谢您抽出时间阅读我的问题,希望我能解决这个问题:D

英文:

i'm new in Android Studio. I want to ask, can i make ImageView with round corners programmatically? because i want use it to load data from database. So, user profile picture will be appear with rounded corner

如何在程序中以编程方式设置ImageView的圆角?

my XML is below :

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;LinearLayout
  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. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. android:orientation=&quot;vertical&quot;
  8. android:background=&quot;#F3FFFC&quot;&gt;
  9. &lt;ScrollView
  10. android:layout_width=&quot;match_parent&quot;
  11. android:layout_height=&quot;match_parent&quot;&gt;
  12. &lt;LinearLayout
  13. android:layout_width=&quot;match_parent&quot;
  14. android:layout_height=&quot;wrap_content&quot;&gt;
  15. &lt;ImageView
  16. android:id=&quot;@+id/imgView&quot;
  17. android:layout_width=&quot;100dp&quot;
  18. android:layout_height=&quot;100dp&quot;
  19. android:src=&quot;@drawable/profilepicture&quot;
  20. /&gt;
  21. &lt;/LinearLayout&gt;
  22. &lt;/ScrollView&gt;
  23. &lt;/LinearLayout&gt;

Thank you for your time to read my question, hope i can solve this problem 如何在程序中以编程方式设置ImageView的圆角?

答案1

得分: 0

  1. <androidx.cardview.widget.CardView
  2. android:layout_width="150dp"
  3. android:layout_height="150dp"
  4. app:cardCornerRadius="20dp">
  5. <ImageView
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:src="@drawable/ic_launcher_background" />
  9. </androidx.cardview.widget.CardView>

使用以下依赖项:implementation 'androidx.cardview:cardview:1.0.0'

英文:
  1. &lt;androidx.cardview.widget.CardView
  2. android:layout_width=&quot;150dp&quot;
  3. android:layout_height=&quot;150dp&quot;
  4. app:cardCornerRadius=&quot;20dp&quot;&gt;
  5. &lt;ImageView
  6. android:layout_width=&quot;match_parent&quot;
  7. android:layout_height=&quot;match_parent&quot;
  8. android:src=&quot;@drawable/ic_launcher_background&quot; /&gt;
  9. &lt;/androidx.cardview.widget.CardView&gt;

by using this dependency implementation &#39;androidx.cardview:cardview:1.0.0&#39;

huangapple
  • 本文由 发表于 2020年7月27日 18:39:55
  • 转载请务必保留本文链接:https://java.coder-hub.com/63113612.html
匿名

发表评论

匿名网友

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

确定