بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]
پرسش و پاسخ

مشکل در جا به جایی و تعویض مکان دو button با یک دیگر (حل شد)

مدیریت گفتگو
Vahid 11 سال پیش
آخرین تغییر: 11 سال پیش

سلام ...
به یه مشکلی بر خوردم که هرچی سعی کردم نتونستم حلش کنم ....

من دو تا عنصر گرافیکی (مثلا button) دارم که داخل یک LinearLayout قرار دارند ...
مثلا به این شکل:

   <LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal" >

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

و من میخوام در برنامه، پس از اینکه کاربر کار خاصی رو انجام داد، جای این دو رو با هم عوض کنم! یعنی btn1 اگر سمت راست btn2 بود بره سمت چپش btn2 و در نتیجه جای این دو با هم عوض شه ...!

حالا موندم چطور چنین کار ساده ای رو باید با کد انجام بدم ...! با عوض کردن Gravity و مقدار دهی های مختلف موفق نشدم کاری کنم ...


ممنون میشم اگر راهنمایی کنید ♥

نظرات

پاسخ‌ها 1

محمد حسین زاده 11 سال پیش

به فرض با داشتن ویوی زیر

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/linearLayout"
android:gravity="center"
android:layout_marginBottom="30dp">

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"/>

<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"/>
</LinearLayout>

<Button
android:id="@+id/btnrtl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="rtl"/>

<Button
android:id="@+id/btnltr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ltr"/>
</LinearLayout>

و کد های اکتیویتی به این شکل

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button btnrtl=(Button)findViewById(R.id.btnrtl);
Button btnltr=(Button)findViewById(R.id.btnltr);
final Button btn1=(Button)findViewById(R.id.btn1);
final Button btn2=(Button)findViewById(R.id.btn2);

final LinearLayout linearLayout=(LinearLayout)findViewById(R.id.linearLayout);

btnrtl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

linearLayout.removeAllViews();
linearLayout.addView(btn1);
linearLayout.addView(btn2);
}
});

btnltr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

linearLayout.removeAllViews();
linearLayout.addView(btn2);
linearLayout.addView(btn1);
}
});
}

میشه کاری که میخوای رو انجام بدی

نظرات

Vahid 11 سال پیش
مرسی جناب حسین زاده مشکلم کاملا رفع شد

پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .