gpt4 book ai didi

Android 以编程方式设计布局

转载 作者:行者123 更新时间:2023-11-29 21:05:16 24 4
gpt4 key购买 nike

enter image description here

如何以编程方式在 android 中制作此布局。

我只想要一个想法,而不是整个编码。

最佳答案

这是答案,但不是直接答案。这就是我个人以编程方式创建复杂布局的方式。

在 XML 中创建相同的布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/some_big_image" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#DD000000" >

<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView2"
android:layout_toRightOf="@+id/imageView2"
android:text="TextView"
android:textColor="@android:color/white" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView2"
android:layout_toRightOf="@+id/imageView2"
android:text="TextView"
android:textColor="@android:color/white" />
</RelativeLayout>

现在从top parentchild

    RelativeLayout mParent = new RelativeLayout(this);
RelativeLayout.LayoutParams mParentParams = new RelativeLayout.LayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
mParent.setLayoutParams(mParentParams);

ImageView mBigImageView = new ImageView(this);
mBigImageView.setLayoutParams(mParentParams);
mParent.addView(mBigImageView);

随着您练习,您可以直接编写相同的代码而无需创建 xml。

关于Android 以编程方式设计布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24796790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com