我有这个AutoCompleteTextView:
AutoCompleteTextView a = new AutoCompleteTextView(this);
Run Code Online (Sandbox Code Playgroud)
我需要将completionThreshold标志值设置为1,我想学习使用java代码.我搜索谷歌和stackoverflow,我没有找到方法.
我只需要这样做: android:completionThreshold="1"使用java代码.
如何才能做到这一点?
android autocompletetextview android-layout layoutparams android-view
我看到这种奇怪的行为,并想问是否有人找到了解决方法.我有一个简单的RelativeLayout,里面有另一个布局.
奇怪的行为发生在代码中.如果我从嵌套布局中获取LayoutParams并将其设置回它,则会改变布局的外观.似乎某些参数在此过程中丢失了.这是代码:
RelativeLayout v = (RelativeLayout)findViewById(R.id.group1);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(v.getLayoutParams());
v.setLayoutParams(lp);
Run Code Online (Sandbox Code Playgroud)
在这个调用之后,我期待一切都保持不变,但是,我正在失去"layout_AlignParentRight"效果(至少).谁能帮我理解为什么会这样?
从实际角度来看,我所需要的只是在运行时更改group1布局的宽度.所以我通过改变上面"lp"的宽度来做到这一点,并且它可以工作,但会弄乱对齐.因此,如果有任何关于在不影响其他属性的情况下更改布局大小的替代方法的建议,请也请插入.
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/group1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentRight="true">
<TextView
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test">
</TextView>
</RelativeLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud) 我吼道xml.在此xml,我需要的是一套MarginTop "@+id/imgFooter" programatically对"@+id/imgCenter".但我不能.我的XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/rLayout"
android:background="@drawable/backrepeat">
<ImageView
android:id="@+id/imgHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="One"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
android:src="@drawable/header" />
<ImageView
android:id="@+id/imgCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imgHeader"
android:layout_above="@+id/imgFooter"
android:contentDescription="Two"
android:src="@drawable/center" />
<ImageView
android:id="@+id/imgFooter"
android:contentDescription="Three"
android:layout_alignParentBottom="true"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/footer" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我的MainActivity.class:
public class StartPage extends Activity{
ImageView imgHeader;
ImageView imgFooter;
DisplayMetrics metrics;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_page);
imgHeader = (ImageView) findViewById(R.id.imgHeader);
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics); …Run Code Online (Sandbox Code Playgroud) android relativelayout imageview android-imageview layoutparams
我创建了一个包含6列和60行的网格视图.我可以在哪里添加,移动和删除图像.但是我很难设置列之间的距离(我不希望列之间有任何空格).因为它会改变屏幕尺寸的变化.我为我的手机设置了它,然后在朋友的手机上尝试了它,并且在列之间有10dp.下面是GridView 编辑的xml :如果我在一个较小的手机中尝试将图像放大到适合单元格的位置.
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image_grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:numColumns="@integer/num_columns"
android:stretchMode="columnWidth" />
Run Code Online (Sandbox Code Playgroud)
下面是布局的java代码
ImageCell v = null;
if (convertView == null) {
v = new ImageCell (mContext);
v.setLayoutParams(new GridView.LayoutParams(80, 80));
v.setScaleType(ImageView.ScaleType.CENTER_CROP );
} else {
v = (ImageCell) convertView;
}
Run Code Online (Sandbox Code Playgroud)
我尝试将v.setLayoutParams改为
v.setLayoutParams(new GridView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT ));
Run Code Online (Sandbox Code Playgroud)
和
v.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Run Code Online (Sandbox Code Playgroud)
但是这两个让GridView无法使用.如果有人知道我做错了什么请告诉我,如果有人需要别的东西也要求它(我不能发布截图)
我有一个ImageView我想以编程方式设置的大小。所以我没有选择将不同大小的图像放在各自的drawable文件夹中。
基本上,我想LayoutParams根据屏幕分辨率进行设置,而不是执行我在下面所做的操作。如果设备是ldpi或mdpi,那么100,100...如果是hdpi,那么150,150等等。我怎样才能做到这一点?
ImageVIew imageView = new ImageView(this);
imageView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
layout.add(imageView);
Run Code Online (Sandbox Code Playgroud) 我想添加textview到listview页眉和给左边距它.这是我做的:
//create a textview, not inflating from layout
TextView selectAdressText = new TextView(getContext());
selectAdressText.setTextSize(12);
selectAdressText.setTextColor(getResources().getColor(R.color.text_black));
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 0, 10, 10);
selectAdressText.setLayoutParams(lp);
addressesLV.addHeaderView(selectAdressText);
Run Code Online (Sandbox Code Playgroud)
但它给出了nullpointerexception.我也试过AbsListViewLayoutparams而不是LinearLayoutLayoutParams但它没有setMargins方法.那LayoutParams我应该用哪个呢?
谢谢
我试图从二维字符串数组创建一个表.但是,我厌倦了在代码中创建视图,因此出于某种原因,我的图像永远不可见,我的文本视图不会分成多行,而是会删除文本.
另外,我在我的代码中的每个textview上都放了id,当有人点击textview时,我希望我可以使用该ID来识别点击的行.不幸的是,事实并非如此.
有没有什么好方法可以识别正确的排?
问候
for (int current=0; current<cityArr.length; current++) {
TableRow tr = new TableRow(this);
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView labelTV = new TextView(this);
labelTV.setId(current);
labelTV.setText(cityArr[current][0]);
labelTV.setTextColor(Color.BLACK);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
ImageView mapView = new ImageView(this);
mapView.setImageResource(R.drawable.list_icon_map);
mapView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT, 5));
tr.addView(mapView);
tlcity.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
Run Code Online (Sandbox Code Playgroud) 这段代码正在扼杀我.我试图以LinearLayout编程方式设置(包含子项)的宽度和高度,应用程序正在打破.这是代码:
LinearLayout ll = (LinearLayout) findViewById(R.id.right_t);
ll.setLayoutParams(new android.widget.LinearLayout.LayoutParams(200, 200));
Run Code Online (Sandbox Code Playgroud)
如你所见,我100%确定我可以使用正确的布局框架.然而,该应用程序打破了错误
1-22 19:54:13.135: ERROR/AndroidRuntime(4560): FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1317)
at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1264)
at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:292)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:314)
at android.view.View.measure(View.java:12863)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12863)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12863)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2256)
at android.view.View.measure(View.java:12863)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1197)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2585)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4507)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native …Run Code Online (Sandbox Code Playgroud) 我是android编程的新手。
我只是发现xml不是设置布局的唯一方法。因此,我试图了解以编程方式设置的布局。我一直在尝试更改按钮的位置。我如何设置布局重力,以便将按钮定位在预期的轴上,比方说 bottom和center?
顺便说一句,是icicle同样的事情savedInstanceState?
这是我偶然发现的编码。您能告诉我设置重力的正确方法吗?
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout one = new LinearLayout(this);
myButton1 = new Button1(this);
one.addView(myButton1,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
myButton2 = new Button2(this);
one.addView(mButton2,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
setContentView(one);
Run Code Online (Sandbox Code Playgroud) 我使用CoordinatorLayout作为我在activity.xml中的根视图.在那个xml中,我使用NestedScrollView并启用了appbar_scrolling_view_behavior.但是NestedScrollView中的内容包装高度而不是匹配父级.
这是我的布局xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways|snap">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content"
android:padding="20dp"
android:visibility="gone"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
android ×10
layoutparams ×10
imageview ×2
android-view ×1
gravity ×1
gridview ×1
image ×1
java ×1
layout ×1
listview ×1
scrollview ×1
tablelayout ×1
tablerow ×1
xml ×1