gpt4 book ai didi

android - 如何以编程方式将多个 LinearLayouts 添加到一个 View 中,然后添加到 ViewFlipper?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:07:12 26 4
gpt4 key购买 nike

我希望问题标题能很好地描述问题。

我想以编程方式创建此 XML(请不要建议不要以编程方式创建 ^_^ )

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:text="Messages:"/>

<EditText android:id="@+id/messageHistory"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:layout_weight="1"
android:editable="false"
android:gravity="top"
android:scrollbars="vertical"
android:scrollbarSize="10px"
/>

<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4">

<EditText android:id="@+id/message"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:layout_weight="1"
/>

<Button android:id="@+id/sendMessageButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4"
android:text="Send"/>

</LinearLayout>

如您所见,有两个 LinearLayout,一个嵌入另一个。我需要重现它,然后将它添加到 ViewFlipper。

这是我目前所拥有的:

     LinearLayout l1 = new LinearLayout(this);
LinearLayout l2 = new LinearLayout(this);
Button btn=new Button(this);
EditText messageHistory = new EditText(this);
EditText newMessage = new EditText(this);
TextView windowTitle = new TextView(this);
btn.setText("Send");
btn.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 4f));

btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Clicked send in child index: "+ flipper.getDisplayedChild(), Toast.LENGTH_SHORT).show();
}
});

windowTitle.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
windowTitle.setPadding(0, 5, 0, 10);
windowTitle.setText("Chat with: ");

messageHistory.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
messageHistory.setGravity(Gravity.TOP);
messageHistory.setMovementMethod(new ScrollingMovementMethod());
messageHistory.setClickable(false);
messageHistory.setFocusable(false);
messageHistory.setPadding(0, 0, 0, 5);


newMessage.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f));
newMessage.setGravity(Gravity.TOP);
newMessage.setMovementMethod(new ScrollingMovementMethod());
newMessage.requestFocus();

l1.setOrientation(LinearLayout.VERTICAL);
l1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
l2.setOrientation(LinearLayout.HORIZONTAL);
l2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

l1.addView(windowTitle);
l1.addView(messageHistory);
l2.addView(newMessage);
l2.addView(btn);
l1.addView(l2);
flipper.addView(l1);

其中 flipper 定义为:

ViewFlipper flipper = (ViewFlipper) findViewById(R.id.viewflip);

没有问题,加载时我可以在窗口中看到“l1”,但找不到 l2。我弄乱了 LayoutParams 吗?我可以使用 addView 添加 LinearLayout 吗?

最佳答案

我认为您忘记设置布局权重(虽然只是我的意见,可能是错误的)。因为您要添加更多布局高度设置为 FILL_PARENT 的 View

关于android - 如何以编程方式将多个 LinearLayouts 添加到一个 View 中,然后添加到 ViewFlipper?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4479429/

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