gpt4 book ai didi

java - Android:创建自定义RelativeLayout

转载 作者:太空宇宙 更新时间:2023-11-04 12:38:06 47 4
gpt4 key购买 nike

我有这个观点

<?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">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView10"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView11"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView12"
android:layout_below="@+id/textView11"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="55dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView13"
android:layout_below="@+id/textView11"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

我想创建一个RelativeLayout的自定义子类,每个添加的项目都是这个 View ,添加它们将像这样:myCustomRelativeLayout.addItem("text1", "text2", "text3", "text4", marginTop, height),我该怎么做?

如果我添加 3 个这样的 View ,它会是这样的:

myCustomRelativeLayout.addItem("left1", "left one", "right1","right one", o, 200);
myCustomRelativeLayout.addItem("left2", "left two", "right2","right two", 150, 200);
myCustomRelativeLayout.addItem("left3", "left three", "right3","right three", 350, 200);

我不是专家,欢迎详细解释

最佳答案

您可以通过使用这些文本创建自定义 View 并将该自定义 View 添加到另一个布局中来实现:

public class CustomView extends RelativeLayout implements View.OnClickListener{

public TextView ClassName, Room, StartingTime, EndingTime;
public float Size;
public RelativeLayout mContainer;

// constructor
public CustomView (Context context, String subject, String room, String startingTime, String endingTime, int background, double size) {
super(context);
init(context);
ClassName.setText(subject );
Room.setText(room );
StartingTime.setText(startingTime);
EndingTime.setText(endingTime);
mContainer.setBackgroundColor(background);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int)size);
params.setMargins(3,0,3,0);
mContainer.setLayoutParams(params2);
}


private void init(Context context) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.my_view, this, true);

this.mContainer = (RelativeLayout)findViewById(R.id.ClassContainer);
this.ClassName = (TextView)findViewById(R.id.theClassName);
this.Room = (TextView)findViewById(R.id.theClassRoom);
this.StartingTime = (TextView)findViewById(R.id.theClassStartingTime);
this.EndingTime = (TextView)findViewById(R.id.theClassEndingTime);
}

创建一个这样的:

CustomView mClass = new Schedule_Class_mini(getActivity(),
"Math",
"Room 8",
"10:30",
"12:00",
Color.parseColor("#508ACCA4"),
150);

Container.addView(mClass, params);

关于java - Android:创建自定义RelativeLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37106152/

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