listViews和图像的实现

时间:2016-01-25 07:30:17

标签: java android listview android-fragments

我的问题是,我想实现一个带有列表和图像的活动,并且在图像的底部有四个文本。当我要打开我的应用程序时,它必须显示一个选定的文本及其相关图像和列表默认情况下,当点击文本的其余部分时,它必须分别在相同的活动中显示他们的相关列表和图像。因为我是新手android,请帮助我。

以下是xml代码

<RelativeLayout
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="2.5"

    >
    <ImageView
        android:id="@+id/mainimage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/pestf"/>



    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:orientation="horizontal"
        android:layout_gravity="bottom"
        android:layout_alignParentBottom="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Pest Control"
            android:id="@+id/pestf"
            android:onClick="click1"
            android:clickable="true"

            android:textStyle="normal"
            android:textSize="15dp"
            android:textColor="#cf0c07"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/packf"
            android:layout_marginLeft="5dp"
            android:id="@+id/packf"
            android:onClick="click1"
            android:clickable="true"

            android:textStyle="normal"
            android:textSize="15dp"
            android:textColor="#cf0c07"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cleaning"
            android:layout_marginLeft="5dp"
            android:id="@+id/cleanf"
            android:onClick="click1"
            android:clickable="true"

            android:textStyle="normal"
            android:textSize="15dp"
            android:textColor="#cf0c07"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Landscaping"

            android:id="@+id/landf"
            android:onClick="click1"
            android:clickable="true"
            android:layout_marginLeft="5dp"
            android:textStyle="normal"
            android:textSize="15dp"
            android:textColor="#cf0c07"/>
    </LinearLayout>
</RelativeLayout>

<ListView
    android:id="@+id/lst"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/ll_pest"
    android:layout_weight="2.2"
    android:background="#FFFFFF"
    >

</ListView>
<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/lst"
    android:layout_weight="3.5"
    android:gravity="center"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="3dp"
    android:layout_alignParentBottom="true"
    android:background="@drawable/border_layout">


    <TextView
        android:id="@+id/txt_pest_Email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#FFFFFF"
        android:text="@string/btn7"

        android:drawablePadding="5dp"
        android:drawableTop="@drawable/icon7"
        android:textStyle="bold"
        android:textColor="#000000"
        />

    <View
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:background="#82020202" />

    <TextView
        android:id="@+id/txt_pest_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#ffffff"
        android:text="@string/btn8"
        android:drawablePadding="5dp"
        android:drawableTop="@drawable/icon8"
        android:textStyle="bold"
        android:textColor="#000000"
        />

</LinearLayout>

这是活动代码

public class demo2 extends AppCompatActivity {


    TextView tv1,tv2,tv3,tv4,txtEmail,txtPhone;
    ListView lv;
    ImageView img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo2);
        String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
        lv = (ListView) findViewById(R.id.lst);
        img = (ImageView) findViewById(R.id.mainimage);
        txtEmail=(TextView)findViewById(R.id.txt_pest_Email);
         txtPhone=(TextView)findViewById(R.id.txt_pest_phone);
        tv1 = (TextView) findViewById(R.id.text1);
        tv2 = (TextView) findViewById(R.id.text2);
        tv3 = (TextView) findViewById(R.id.text3);
        tv4 = (TextView) findViewById(R.id.text4);
        lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, adobe_products));
        tv1.setTextColor(getResources().getColor(R.color.blue));

        txtPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mobileNo = "08042589999";
                String uri = "tel:" + mobileNo.trim();
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse(uri));
                startActivity(intent);
            }
        });

        txtEmail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(demo2.this, Booknow.class);
                // sending data to new activity
                i.putExtra("flag",true);
                i.putExtra("cat",1);
                startActivity(i);
            }
        });
        // Inflate the layout for this fragment

    }







    public void click2(View v){
        img.setBackgroundResource(R.drawable.packers);
        String[] countries = getResources().getStringArray(R.array.country);
        lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item1, countries));
        tv2.setTextColor(getResources().getColor(R.color.blue));
        tv1.setTextColor(getResources().getColor(R.color.red));
        tv3.setTextColor(getResources().getColor(R.color.red));
        tv4.setTextColor(getResources().getColor(R.color.red));
    }

    public void click1(View v){

        img.setBackgroundResource(R.drawable.pestf);
        String[] adobe = getResources().getStringArray(R.array.adobe_products);
        lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item1, adobe));
        tv1.setTextColor(getResources().getColor(R.color.blue));
        tv2.setTextColor(getResources().getColor(R.color.red));
        tv3.setTextColor(getResources().getColor(R.color.red));
        tv4.setTextColor(getResources().getColor(R.color.red));
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {


                // Launching new Activity on selecting single List Item
                Intent i = new Intent(demo2.this, BedbugControl.class);
                // sending data to new activity
                i.putExtra("position", position);
                startActivity(i);

            }
        });

    }
    public void click3(View v){

        img.setBackgroundResource(R.drawable.cleaningf);
        String[] sweets = getResources().getStringArray(R.array.sweets);
        lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item1, sweets));
        tv3.setTextColor(getResources().getColor(R.color.blue));
        tv1.setTextColor(getResources().getColor(R.color.red));
        tv2.setTextColor(getResources().getColor(R.color.red));

        tv4.setTextColor(getResources().getColor(R.color.red));
    }
    public void click4(View v) {
        img.setBackgroundResource(R.drawable.landf);
        String[] companies = getResources().getStringArray(R.array.company);
        lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item1, companies));
        tv4.setTextColor(getResources().getColor(R.color.blue));
        tv1.setTextColor(getResources().getColor(R.color.red));
        tv2.setTextColor(getResources().getColor(R.color.red));
        tv3.setTextColor(getResources().getColor(R.color.red));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_demo2, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

0 个答案:

没有答案