gpt4 book ai didi

java - 将按钮/编辑文本添加到相对布局(膨胀)

转载 作者:行者123 更新时间:2023-12-01 15:13:40 27 4
gpt4 key购买 nike

我的代码有问题:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);

// Change icon based on name
String s = values[position];

System.out.println("s = "+s);
System.out.println("Results = "+name[2].toString());
for (int i = 0; i < name.length; i ++){
if (s.equals(name[i])) {
imageView.setImageDrawable(loadImageFromURL("http://10.0.0.117/test/"+s+".jpg", "iOS"));
}
}
return rowView;
}

这是我的 XML:

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

<ImageView
android:id="@+id/logo"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:contentDescription="@string/desc"
android:src="@drawable/windowsmobile_logo" >
</ImageView>

<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/logo"
android:layout_marginBottom="16dp"
android:layout_toRightOf="@+id/logo"
android:text="@+id/label"
android:textSize="20dp" />


<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >



<Button
android:id="@+id/btn_zoeken"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/txt_zoeken"
android:text="@string/Zoeken" />

<EditText
android:id="@+id/txt_zoeken"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >

<requestFocus />
</EditText>
</RelativeLayout>

</RelativeLayout>

我遇到了这个问题:
我有一个列表,在屏幕底部,它们应该始终是一个 EditText 和一个 按钮。但是,当我将其添加到布局中并启动应用程序时,它会将 EditTextbutton 放置在每个 处,请参阅图片:

事情就是这样(不好):

How it is now (bad)

事情应该是这样的:

How it should be

我在布局中加入了 RelativeLayout in a RelativeLayout,因为我希望它能解决这个问题,但事实并非如此。

请注意,我在相对布局中创建了一个列表,因此我不使用 ListView。
有人有解决方案或建议如何解决这个问题吗?

我希望我的问题很清楚。

谢谢 Bigflow

编辑1:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View rowView = inflater.inflate(R.layout.inflater, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
Button btn_zoeken = (Button) rowView.findViewById(R.id.button1);
textView.setText(values[position]);

// Change icon based on name
String s = values[position];

System.out.println("s = "+s);
System.out.println("Results = "+name[2].toString());
for (int i = 0; i < name.length; i ++){
if (s.equals(name[i])) {
imageView.setImageDrawable(loadImageFromURL("http://10.0.0.117/test/"+s+".jpg", "iOS"));
}
}
return rowView;
}

最佳答案

按照 Mohasin Naeem 的建议,使用 main.xml 文件中的按钮(即 btn_zoeken)和图像(即 txt_zoeken)作为页脚。并使用此 android :layout_alignParentBottom="true"

并将您的 list_mobile.xml 扩展到 main.xml 布局文件。

因此您的 list_mobile.xml 应该如下所示。

 <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/button1" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Button" />

</RelativeLayout>

btn_zoeken 和 txt_zoeken 应位于主屏幕底部。你的膨胀的layout.xml应该像下面这样。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >

<ImageView
android:id="@+id/logo"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:contentDescription="@string/desc"
android:src="@drawable/windowsmobile_logo" >
</ImageView>

<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/logo"
android:layout_marginBottom="16dp"
android:layout_toRightOf="@+id/logo"
android:text="@+id/label"
android:textSize="20dp" />
</RelativeLayout>

基本上,在 inflated layout 时,您需要两个 xml 文件。一个是您的 main.xml(list_mobile.xml),另一个是您的膨胀 xml

关于java - 将按钮/编辑文本添加到相对布局(膨胀),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11947625/

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