我有像这样的线性布局
<LinearLayout
android:id="@+id/optionlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/divider"
android:orientation="vertical" >
</LinearLayout>
我想在此布局中添加另一个xml视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!--
<View
android:layout_width="match_parent"
android:layout_height="0.5dip"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#2fd275"
/>
-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >
<ImageView
android:id="@+id/option_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/logout" />
<TextView
android:id="@+id/option_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:visibility="gone" />
<TextView
android:id="@+id/option_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:text="Signout"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#616161" />
</LinearLayout>
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="0.5dip"
android:background="#d3d3d3" />
</LinearLayout>
通过代码
for (int i = 0; i < opt.length; i++) {
View view = inflater.inflate(R.layout.option_item, optionlist,
false);
view.setBackgroundResource(R.drawable.optionlist);
view.setTag(opt_image[i]);
view.setClickable(true);
view.setFocusable(true);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// ADD your action here
int res = (Integer) v.getTag();
switch (res) {
case R.drawable.logout:
signout();
break;
}
}
});
final TextView tv = (TextView) view.findViewById(R.id.option_name);
final ImageView iv = (ImageView) view
.findViewById(R.id.option_image);
tv.setText(opt[i]);
iv.setImageResource(opt_image[i]);
optionlist.addView(view);
}
现在点击布局我想加载这个xml文件,如下所示...但我无法在点击事件中加载背景。请提出建议我做错了什么?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/holo_green_dark" android:state_focused="true"/>
<item android:drawable="@color/holo_green_dark" android:state_enabled="false" android:state_pressed="true"/>
<item android:drawable="@color/white"/>
</selector>
答案 0 :(得分:3)
首先从xml获取LinearLayout的id,然后在其上执行onClickListener而不是整个View
View view = inflater.inflate(R.layout.option_item, optionlist, false);
LinearLayout layout = view.findViewByIt(R.id.optionlist);
layout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// ADD your action here
int res=(Integer) v.getTag();
switch(res)
{
case R.drawable.logout: signout();
break;
}
}
});
答案 1 :(得分:2)
试试这个..希望它有效..
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#ff0000" />//ur onclick desired color
<item android:state_focused="false"
android:drawable="@android:color/transparent" />focused color
</selector>
只需创建新的xml并将此代码添加到drawable中并将其设置为线性layoyut backgrout,其名称为android:background="@drawable/createdxmlname"
<LinearLayout
android:id="@+id/optionlist"
android:layout_below="@id/divider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/createdxmlname"
android:clickable="true">
答案 2 :(得分:0)
android:clickable="false"
<LinearLayout
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:clickable="false"
android:layout_width="40dp"
android:layout_height="40dp"
android:text="Cinema"
android:textSize="13sp"
android:layout_marginRight="10dp" />
<Button
android:clickable="false"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Check in"
android:textSize="13sp" />
</LinearLayout>
答案 3 :(得分:0)
在drawable中创建一个带有以下内容的pressed_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/button_focused" /> <!-- focused -->
<item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>
在您的布局中将其添加为背景资源:
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/pressed_layout"
android:clickable="true">
</LinearLayout>
答案 4 :(得分:0)
layout.setclickble =“true”和
layout.setOnLongClickListener(new View.OnLongClickListener(){
Get-Content 'c:\temp\ps\pc.txt' | ForEach-Object {
...
} | ConvertTo-Csv -NoType | Tee-Object -FilePath 'C:\temp\ps\output.txt'
答案 5 :(得分:-1)
我们也可以在布局的XML中使用onClick,并在java文件中使用该方法,如onClick =&#34; next&#34;,然后在java文件中我们必须使用该方法。
// get the rows that contain proxies
var proxies = htmlDoc.DocumentNode.SelectNodes("//tr[td/a[@title='Proxy details']]");
if (proxies != null)
{
foreach (var proxy in proxies)
{
// process this proxy row
Console.WriteLine("IP:" + proxy.SelectSingleNode(td/a[@title='Proxy details']).InnerText);
Console.WriteLine("PORT:" + proxy.SelectSingleNode(td[2]).InnerText);
}
}
Console.ReadLine();