假设我有10个没有.java
文件的布局。我想使用按钮调用所有布局。是否可以只使用一个MainActivity
.java
文件意味着我们可以使用多个setContentView
吗?
答案 0 :(得分:0)
您不能为任何活动提供多个布局,但如果您想在一个活动中使用不同的布局文件,则可以使用标记将所有布局文件包含到单个布局文件中,并在活动中使用它们。 / p>
答案 1 :(得分:0)
在主布局中包含所有布局,并为所有其他布局制作<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!-- main layout for first time loading activity-->
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<!-- Display layout based on button click-->
<LinearLayout
android:visibility="gone"
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<!-- Display layout based on button click-->
<LinearLayout
android:visibility="gone"
android:id="@+id/layout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<!-- Display layout based on button click-->
<LinearLayout
android:visibility="gone"
android:id="@+id/layout4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
。并且基于按钮单击,您可以根据布局ID显示任何特定布局。
示例xml如下:
{{1}}
答案 2 :(得分:0)
typedef enum statucEnum {CALLED_AHEAD, WAITING} status;
typedef struct nodeStruct{
char* name;
int groupSize;
status inStatus;//in resturant status
struct nodeStruct* next;
}Node;
//structure to encapsulate the head of the queue along with the tail
typedef struct headStruct{
Node* head;
Node* tail;
}Queue;
void intializeQueue(Queue* queue){
queue->head = NULL;
queue->tail = NULL;
}
int main(){
Queue queue;
intializeQueue(&queue);
//program ask what the user wants to do, and it decides to add to the queue
doAdd(&queue);
}
void doAdd(Queue* queue){
//usually the program would ask the user to input a age and name
int age = 4;
name = "v";
Node* newNode;
newNode = malloc(sizeof(Node));
newNode->groupSize = size;
newNode->name = name;
newNode->inStatus = WAITING;
addToList(queue, newNode);
}
标题栏是不同的布局
void addToList(Queue* queue, Node* node){
printf("Address of parameter: %p", node);
if (queue->head == NULL){
queue->head->next = node; \\this is where the error occurs
queue->tail->next = node;
}else{
queue->tail->next = node;
queue->tail = node;
}
}
包括像这样的主要布局..
const Observable = Rx.Observable
// Define tests here, allow requestAgain() to pass one
const testUser = (user) => user.id % 5 !== 0
const testUserAllowOneSuccess = (user) => user.id === 5 || user.id % 5 !== 0
const requestAgain = (login) => Observable.of(login)
.switchMap(login => jQuery.getJSON("https://api.github.com/users/" + login ))
.map(x => {
if(!testUserAllowOneSuccess(x)) {
console.log("logging error retry attempt", x.id);
throw new Error('Invalid user: ' + x.id)
}
return x;
})
.retry(3)
.catch(error => Observable.of(error))
const userStream = Observable.fromPromise(jQuery.getJSON("https://api.github.com/users"))
.concatAll()
const passed = userStream.filter(x => testUser(x))
const failed = userStream.filter(x => !testUser(x))
.flatMap(x => requestAgain(x.login))
const retryPassed = failed.filter(x => !(x instanceof Error))
const retryFailed = failed.filter(x => (x instanceof Error))
.toArray()
.map(errors => { throw errors })
const output = passed.concat(retryPassed, retryFailed)
output.subscribe(
x=> console.log('subscribe next', x.id ? x.id : x),
e => console.log('subscribe error', e)
);
答案 3 :(得分:0)
您可以使用片段。
只需在主活动布局中声明一个FrameLayout。
为每个所需的布局创建片段。
现在,您可以使用Fragment Manager切换布局。
有关详细信息,请参阅此处:Here
希望它能帮助!!
答案 4 :(得分:0)
不,您不能将多个setContentView()
用于单个活动。
如果您只想将其他xml布局添加到另一个。您可以使用<include>
标记。
示例:
<强> layout_one.xml:强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is layout one"/>
</LinearLayout>
<强> layout_two.xml:强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is layout two"/>
</LinearLayout>
<强> activity_main.xml中:强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<include layout="@layout/layout_one">/>
<include layout="@layout/layout_two">/>
</LinearLayout>