Android FrameLayout example
With FrameLayout, we can ensure that the android components overlap. For example, if you have a pic, we want you to write a name on it or write a score, then it would be the most logical solution if we use FrameLayout.
Layout ,XML
- Set the image in xml according to yourself and drop it into drawable
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="center" android:src="@drawable/img" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_gravity="center_horizontal|bottom" android:padding="10dp" android:background="#cccc" android:textColor="#ffffffff" android:text="Jungle" /> </FrameLayout> |
- screen output
Dynamic creation
- We can dynamically adjust the objects we create and place with xml as follows
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
public class MainActivity extends AppCompatActivity { final Context context = this; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); ImageView img = new ImageView(this); FrameLayout.LayoutParams imageParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT); img.setLayoutParams(imageParams); TextView txt = new TextView(this); txt.setText("Jungle"); txt.setTextColor(Color.parseColor("#FFFFFF")); txt.setPadding(10,10,10,10); FrameLayout.LayoutParams txtParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT); txtParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; txt.setLayoutParams(txtParams); FrameLayout outsideLayout = new FrameLayout(this); outsideLayout.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); outsideLayout.addView(img); outsideLayout.addView(txt); setContentView(outsideLayout); } } |
Demo
- Our screen output will still look like this
if you have question do not forget to write from the chat button next to it or from the comment
Recent Comments