我找不到这样做的方法,但有没有办法重置项目上的LayoutParams?
在设备旋转时,我改变了布局参数.当我回到肖像时,我想将它们更改回XML值.我是否只需要定义与XML中的内容匹配的新布局参数?
我在对话框中制作了一个edittext但我无法改变高度.
AlertDialog.Builder editalert = new AlertDialog.Builder(this);
editalert.setTitle("messagetitle");
editalert.setMessage("here is the message");
final EditText input = new EditText(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
input.setLayoutParams(lp);
editalert.setView(input);
editalert.setPositiveButton("Send via email", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
editalert.show();
Run Code Online (Sandbox Code Playgroud) 这是我的第一篇文章.我正在学习java for Android应用程序的代码.我正在使用一个LinearLayout"变量名为:ll"与预定义的layout_weight(我想在将来随时更改)和高度和宽度设置为MATCH_PARENT.我想以像素为单位获得此LinearLayout的高度.我使用java(ll.addView(variableForAnObject);)填充它.我试图使用ll.getLayoutParams().height获得它的高度; 但我得到了-1.因为我相信这是MATCH_PARENT的常量.将高度设置为0dp会使ll不可见,并将高度设置为0.
请告诉我是否有办法达到它的高度.我想我可以通过使用DisplayMetrics获取屏幕高度并进行几乎不可能的计算(至少对我而言)来做到这一点,但这太复杂了.
java android android-layout layoutparams android-layout-weight
我正在尝试实现用户可调整大小的edittext视图.当用户拖动视图边缘时,可以调整大小.
这是我用于edittext的onTouchListener.我只实现了左边缘的调整大小.我的想法是,当用户的触摸事件x等于视图的左边距(这意味着用户触摸左边缘)时,视图的宽度和左边距将变化.
protected View.OnTouchListener etTouchListener=new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
float x=event.getX();
float y=event.getY();
ViewGroup.MarginLayoutParams params=(ViewGroup.MarginLayoutParams)view.getLayoutParams();
int top=params.topMargin;
int bottom=params.bottomMargin;
int left=params.leftMargin;
int right=params.rightMargin;
if(x==left){
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
resize_start(x,y);
Log.e("left edge","clicked");
break;
case MotionEvent.ACTION_MOVE:
resize_move(x,y);
params.leftMargin=(int)nX;
params.width=(int)(editNoteViewParent.getWidth()-nX-params.rightMargin);
view.setLayoutParams(params);
Log.e("left edge","dragged");
break;
case MotionEvent.ACTION_UP:
break;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的resizeStart():
private void resize_start(float x,float y){
nX = x;
nY = y;
}
Run Code Online (Sandbox Code Playgroud)
而我的resizeMove():(当用户拖动边缘时)
private void resize_move(float x, float y){
float dx = Math.abs(x - nX);
float dy …Run Code Online (Sandbox Code Playgroud) 似乎有很多关于如何在android上使用拖动视图的解决方案.
然而,它们要么太慢,要么是马车.
一个已知的解决方案是使用framelayout,视图只需在使用OnTouchListener移动时更改其layoutParams,如下所述:http://www.anddev.org/novice-tutorials-f8/android-drag-and-drop -of-normal-views-not-bitmaps-t13404.html 但是,如果视图是imageView,当到达framelayout的最右侧(或最底部)时,图像会缩小.
另一种可能的解决方案是使用画布来显示拖动的内容,如下所述:http: //www.anddev.org/basic_drag_and_drop-t3095.html 但是,我不确定如何将它用于复杂视图.
没有人知道一个很好的解决方案,一个让拖累视图的大小保持不变,不管它是在它的容器?
请帮我 .
当用户在屏幕上向下移动手指时,我想更改视图高度。
new OnTouchListener() {
float lastY = Float.MIN_VALUE;
@Override
public boolean onTouch(View v, MotionEvent event) {
if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_MOVE) {
if (lastY != Float.MIN_VALUE) {
final float dist = event.getRawY() - lastY;
myView.post(new Runnable() {
@Override
public void run() {
ViewGroup.LayoutParams lp = myView.getLayoutParams();
lp.height += dist;
myView.setLayoutParams(lp);
}
});
view.invalidate();
}
lastY = event.getRawY();
}
if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
lastY = Float.MIN_VALUE;
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
但是只有当用户停止移动手指时,视图高度才会改变!
发生 ACTION_MOVE 时如何立即更改视图高度?
使用setLayoutParams和为addView提供参数有什么区别?
我知道addView仅在第一次添加视图时才有效.我找到了两种显然做同样事情的方法:
tv.setLayoutParams(params);
layout.addView(tv)
Run Code Online (Sandbox Code Playgroud)
VS
layout.addView(tv, params)
Run Code Online (Sandbox Code Playgroud)
它们是等价的吗?
如果不是,有什么区别?
在我的申请中,我有一个会话室.我的所有消息都应该正确对齐,而其余消息应保持对齐.
我的列表的单行是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/llContainer" >
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/speech_bubble_orange"
android:id="@+id/llGroup" >
<TextView
android:id="@+id/message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shadowColor="@color/message_textShadow"
android:shadowDx="1"
android:shadowDy="1"
android:text="Medium Text"
android:textColor="@color/message_textColor"
android:textSize="20sp" />
<TextView
android:id="@+id/message_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/temp_date"
android:gravity="right"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"/>
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的适配器的getView()是这样的:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.list_message_row, null);
holder = new ViewHolder();
holder.tvMessageBody = (TextView) convertView.findViewById(R.id.message_text);
holder.tvMessageDate = (TextView) convertView.findViewById(R.id.message_date);
holder.llContainer = (LinearLayout) convertView.findViewById(R.id.llContainer); …Run Code Online (Sandbox Code Playgroud) 我在programmaticaly中创建了按钮,而不是在xml文件中.然后我想在这个链接中设置LayoutParams:如何以编程方式设置相对布局中按钮的layout_align_parent_right属性?
但是当我试图发射时,我有一个例外.
这是我的代码:
RelativeLayout ll2 = new RelativeLayout(this);
//ll2.setOrientation(LinearLayout.HORIZONTAL);
ImageButton go = new ImageButton(this);
go.setId(cursor.getInt(cursor.getColumnIndex("_id")));
go.setClickable(true);
go.setBackgroundResource(R.drawable.go);
RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)go.getLayoutParams();
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); // LogCat said I have Null Pointer Exception in this line
go.setLayoutParams(params1);
go.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i3 = new Intent(RecipesActivity.this, MedicineActivity.class);
i3.putExtra(ShowRecipe, v.getId());
i3.putExtra("Activity", "RecipesActivity");
RecipesActivity.this.startActivity(i3);
}
});
ll2.addView(go);
Run Code Online (Sandbox Code Playgroud)
为什么我的应用会抛出异常?谢谢.
如果我在视图上调用 setPadding() ,我可以成功设置填充,但是,如果我先设置 layoutParams 然后设置填充,或者设置填充然后设置 layoutParams,则不会应用填充。
示范:
//This doesn't work
textView.setLayoutParams(linearLayoutParams);
textView.setPadding(0, 100, 0 , 0);
//This doesn't work either
testView.setPadding(0, 100, 0 , 0);
textView.setLayoutParams(linearLayoutParams);
//This does work
textView.setPadding(0, 100, 0 , 0);
Run Code Online (Sandbox Code Playgroud)
有谁知道如何同时使用 setLayoutParams() 和 setPadding(),或者为什么 setLayoutParams() 会阻止 setPadding() 工作?
编辑:更多细节:
我在 onCreate() 中调用这个方法
public void initTextView(){
Textview textView = (TextView) findViewById(R.id.textView);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(myWidth, myHeight);
textView.setLayoutParams(linearLayoutParams);
//This doesn't work
textView.setPadding(0, 100 ,0 , 0);
}
Run Code Online (Sandbox Code Playgroud)
但如果我从上面注释掉这一行:
// textView.setLayoutParams(linearLayoutParams);
Run Code Online (Sandbox Code Playgroud)
然后 setPadding() 方法起作用了。
提前致谢。
android ×10
layoutparams ×10
java ×2
view ×2
drag ×1
imagebutton ×1
imageview ×1
layout ×1
ontouch ×1
resize ×1
touch ×1
touch-event ×1