Android 以编程方式添加布局+;儿童
我目前正在学习Android编程的基础知识。我可以用一个页面和几个按钮创建一个静态应用程序。下一步是学习动态创建应用程序,就像我在webapplications中习惯的那样 我想做的是创建一个简单的类似facebook的应用程序,其中包含一些时间线对象、一些文本、一个like按钮和放置日期。有一个简单的NodeJS服务器,它用JSON文件响应http GET请求。它被解析,对于数组中的每个对象,都应该创建一个timeline对象,就像facebook上一样 在我的主页上,我正在创建一些动态内容来包含时间线对象Android 以编程方式添加布局+;儿童,android,android-layout,android-linearlayout,android-design-library,Android,Android Layout,Android Linearlayout,Android Design Library,我目前正在学习Android编程的基础知识。我可以用一个页面和几个按钮创建一个静态应用程序。下一步是学习动态创建应用程序,就像我在webapplications中习惯的那样 我想做的是创建一个简单的类似facebook的应用程序,其中包含一些时间线对象、一些文本、一个like按钮和放置日期。有一个简单的NodeJS服务器,它用JSON文件响应http GET请求。它被解析,对于数组中的每个对象,都应该创建一个timeline对象,就像facebook上一样 在我的主页上,我正在创建一些动态内容来
ConstraintLayout container = activity.findViewById(R.id.container);
container.removeAllViews();
TextView nav_txt = activity.findViewById(R.id.nav_txt);
nav_txt.setText("Home");
swipeRefreshLayout = new SwipeRefreshLayout(activity);
ScrollView scrollView = new ScrollView(activity);
container.addView(swipeRefreshLayout);
scrollView.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ConstraintLayout.LayoutParams.MATCH_PARENT));
linearLayout = new LinearLayout(activity);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setId(R.id.linearLayout);
swipeRefreshLayout.addView(scrollView);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshData();
}
});
scrollView.addView(linearLayout);
这很简单,视图不需要任何约束,因为所有对象都与父对象具有相同的高度
时间线对象更复杂,有很多约束和大小不同的子对象,因此我想,我创建了一个自定义XML文件,其中包含已定义的时间线对象,按ID抓取它并修改它以满足我的需要
我的timeline_message.xml如下所示:
<android.support.constraint.ConstraintLayout 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.support.constraint.ConstraintLayout
android:id="@+id/timeline_msg"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="8dp"
android:background="@color/backgroundColor"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/like_btn"
android:layout_width="90dp"
android:layout_height="91dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="@color/colorPrimary"
android:text="Like"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/username"
android:layout_width="272dp"
android:layout_height="55dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:gravity="center_vertical"
android:text="text"
android:textAlignment="center"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/like_btn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/date"
android:layout_width="91dp"
android:layout_height="40dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:gravity="center_vertical"
android:text="TextView"
android:textAlignment="center"
android:textColor="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.003"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/username"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
LinearLayout linearLayout = activity.findViewById(R.id.linearLayout);
ConstraintLayout timelineMsg = activity.findViewById(R.id.timeline_msg);
linearLayout.addView(timelineMsg);
但什么也没发生。这样的方法可能吗?或者你需要用layoutparams和constraints e.t.c.手动编写所有代码吗?你差一点就忘了什么 如果您想使用timeline_message.xml布局,则需要先将其充气 例如:
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
然后,您可以使用您的充气机为视图充气
View view = inflater.inflate(R.layout.timeline_message, null);
现在可以使用视图查找元素
ConstraintLayout timelineMsg = view.findViewById(R.id.timeline_msg);
对于finish,您可以使用将充气视图添加到容器中
linearLayout.addView(view);
你差一点就忘了什么 如果您想使用timeline_message.xml布局,则需要先将其充气 例如:
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
然后,您可以使用您的充气机为视图充气
View view = inflater.inflate(R.layout.timeline_message, null);
现在可以使用视图查找元素
ConstraintLayout timelineMsg = view.findViewById(R.id.timeline_msg);
对于finish,您可以使用将充气视图添加到容器中
linearLayout.addView(view);
嘿timelineMsg属于?嘿timelineMsg属于?