有LinearLayout很多儿童元素.当用户触摸任何这些子元素时,将调用相同的方法.为了不onClickListener为每个元素实现相同,我实现了仅onClickListener用于父LinearLayout .
现在,当我单击父布局边框内的任何位置时,正在调用所需的方法,就像我为所有子元素实现了侦听器一样.
问:我可以随时onClickListener为父级实现它,它的所有子元素都会对click事件做出反应吗?
问:如果任何子元素有自己的元素,会发生什么onClickListener?是否会发生碰撞或点击该元素只会触发自己的点击事件?
我想创建一个线性布局,其行为与ImageButton类似.
<LinearLayout
android:id="@+id/container"
style="?WidgetHomeIconContainer">
<ImageView
android:id="@+id/icon"
style="?WidgetHomeIcon" />
<TextView
android:id="@+id/title"
style="?WidgetHomeLabel"
android:text="@string/title"
android:textAppearance="?attr/TextHomeLabel" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在ImageView,TextView和LinearLayout的样式中,我为所有状态设置了一个选择器.
现在:
所以我想做以下几点.当我点击父LinearLayout时,我需要将它的所有子项更改为按下状态.
我尝试将以下代码添加到LinearLayout onClickListener以传播点击:
@Override
public void onClick(View v)
{
LinearLayout l = (LinearLayout) v;
for(int i = 0; i < l.getChildCount(); i++)
{
l.getChildAt(i).setClickable(true);
l.getChildAt(i).performClick();
}
}
Run Code Online (Sandbox Code Playgroud)
但它仍然保持相同的状态.非常感谢您的帮助.
在Activity中,您可以通过以下方式以编程方式创建LinearLayout:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TextView tv1 = new TextView(this);
tv1.setText("HELLO");
ll.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText("WORLD");
ll.addView(tv2);
setContentView(ll);
}
Run Code Online (Sandbox Code Playgroud)
你如何在自定义View子类中做同样的事情?没有setContentView或onCreate方法......
android android-custom-view android-layout android-linearlayout android-view
<Button
android:id="@+id/o_pharmacy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/p2"
android:text="@string/o_pharmacy"
android:textSize="26sp" />
<Button
android:id="@+id/lab"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/lab"
android:text="@string/lab"
android:textSize="26sp" />
<Button
android:id="@+id/i_pharmacy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/p1"
android:text="@string/i_pharmacy"
android:textSize="26sp" />
Run Code Online (Sandbox Code Playgroud)
我试过上面的代码在Liner布局中显示3个按钮.它工作但我需要在两个按钮之间放置空间.
这个问题源于必须动态地显示/隐藏不同的视图.View有3个可见性设置 - 可见,不可见和消失.如果你有一个父视图,例如a LinearLayout,它有几个子视图(无关紧要)是设置父视图的可见性与在所有子视图上独立设置visiblity相同?例如,如果我说
LinearLayout container = (LinearLayout) findViewById(R.id.layout_1);
container.setVisiblity(View.GONE);
Run Code Online (Sandbox Code Playgroud)
这与查找每个子视图并将所有这些可见性设置为相同View.GONE吗?如果父母不是View.GONE,View.INVISIBLE怎么办?所有的孩子还在画画但是还没有看到吗?
为了给我的应用程序的用户指示哪个字段当前具有焦点我正在尝试根据当前状态更改某些字段的背景颜色,但是,我在理解Androids颜色状态列表资源时遇到了麻烦:
我找到了示例(对不起,URL不再有效),如果我尝试完全相同,即如果我想调整textColor,事情确实有效.但是,如果我尝试的只是略有不同的东西,即相适应的背景颜色,事情不工作,我不明白为什么?为什么这么不一致?
为了更容易理解我想要做什么,我附加了我的混合物..xml文件:
该AndroidManifest.xml文件中:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mmo.android.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Test"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
的Test-Activity:
package mmo.android.test;
import android.app.Activity;
import android.os.Bundle;
public class Test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Run Code Online (Sandbox Code Playgroud)
的res/values/strings.xml: …
这是我的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="5dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="#ffffff" >
</TextView>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/nazajGumb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/roaming_backbtn" >
</Button>
<Button
android:id="@+id/homeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/roaming_homebtn" >
</Button>
</LinearLayout>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dip"
android:prompt="@string/roaming_spinnerPrompt" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Random text"
android:textColor="#ffffcc" />
<Button
android:id="@+id/testBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="test" >
</Button>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
LinearLayout2中的ImageView和TextView的位置以及LinearLayout3中按钮的positiong不起作用(使用布局重力). …
在下面的用户界面中,我在下面可以覆盖整个屏幕.LinearLayout是透明的,允许其下方的控件可点击或触摸.基本上我可以滚动此LinearLayout下面的列表以及单击控件.我如何禁用它?
见附例.
谢谢
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlExtNavbar"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill_vertical"
android:layout_gravity="fill_vertical"
android:background="@color/transparent" xmlns:tools="http://schemas.android.com/tools" tools:ignore="Overdraw">
<RelativeLayout
android:id="@+id/expandedNavbarLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</RelativeLayout>
<LinearLayout
android:id="@+id/transparentLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/expandedNavbarLayout"
android:focusableInTouchMode="false"
android:focusable="false"
android:background="@color/fulltransparent">
</LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud) 背景:
这就是为什么我决定创建我自己的布局,你要告诉每个孩子他们的重量(和周围的重量)与它的大小相比应该是什么.
看来我已经成功了,但出于某种原因,我认为有一些我无法追查的错误.
其中一个错误就是textView,即使我为它提供了大量空间,它也会将文本放在顶部而不是放在中心.另一方面,imageViews工作得很好.另一个错误是,如果我在自定义布局中使用布局(例如frameLayout),则其中的视图将不会显示(但布局本身将会显示).
请帮我弄清楚它为什么会发生.
如何使用:而不是线性布局的下一个用法(我故意使用长XML,以显示我的解决方案如何缩短事物):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<View android:layout_width="wrap_content" android:layout_height="0px"
android:layout_weight="1" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="0px" android:layout_weight="1"
android:orientation="horizontal">
<View android:layout_width="0px" android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView android:layout_width="0px" android:layout_weight="1"
android:layout_height="match_parent" android:text="@string/hello_world"
android:background="#ffff0000" android:gravity="center"
android:textSize="20dp" android:textColor="#ff000000" />
<View android:layout_width="0px" android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<View android:layout_width="wrap_content" android:layout_height="0px"
android:layout_weight="1" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我所做的只是(x是将视图本身放在权重列表中的位置):
<com.example.weightedlayouttest.WeightedLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.weightedlayouttest"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
<TextView android:layout_width="0px" android:layout_height="0px"
app:horizontalWeights="1,1x,1" app:verticalWeights="1,1x,1"
android:text="@string/hello_world" android:background="#ffff0000"
android:gravity="center" android:textSize="20dp" android:textColor="#ff000000" />
</com.example.weightedlayouttest.WeightedLayout>
Run Code Online (Sandbox Code Playgroud)
我的特殊布局代码是:
public class WeightedLayout extends ViewGroup
{
@Override …Run Code Online (Sandbox Code Playgroud) android android-layout android-linearlayout android-layout-weight android-percent-library
我最近又遇到了一个问题,我在过去几年里曾多次遇到这个问题.
LinearLayout很方便layout manager.但我完全错过的是在单个XML标记中添加元素之间的某个空间(如填充)的可能性.
我的意思是,我可以在LinearLayout的声明中定义元素之间的间距(例如,在垂直LinearLayout中,这个布局中两个元素之间的垂直空间).
我知道我可以通过添加XML标签android:layout_marginTop或类似于LinearLayout中每个元素的东西来实现.
但我希望能够在一个点上定义它,因为所有元素的间距都是相同的.
有没有人知道一个简单的方法(不实现自定义LinearLayout或类似的东西)?我更喜欢直接在XML中工作而无需编码的解决方案.